Hi all..

I am having some controls in my aspx page.I need to write javascript for those controls for validation..For that i need to make the controls visible false in certain situation .I also want to make the controls to visible true in certain sitauations..Some one help me with javascript code for this..

Regards,
Balagurunathan S

There are many ways to show/hide, render/not render a control
For ASP.NET webcontrols you have the Visible property which can be set to true or false but that's serverside.

For client side you have visibility attribute in CSS which can be "visible" or "hidden", here is an example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ControlVisibilityClientSide.aspx.cs" Inherits="ControlVisibilityClientSide" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ControlVisibilityClientSide</title>
    <script type="text/javascript">
        function toggleVisibility(controlId)
        {
            var control = document.getElementById(controlId);
            if(control.style.visibility == "visible" || control.style.visibility == "")
                control.style.visibility = "hidden";
            else 
                control.style.visibility = "visible";
              
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <input type="button" ID="btnShowHide" value="Show/Hide" onclick="toggleVisibility('TextBox1');" /></div>
    </form>
</body>
</html>

Thanks Holly..I got it..


Regards,
Balagurunathan S

Is it possible to use asp.net button instead of html button?

regrads
saars

Yes it is, an asp:Button will cause a postback and you can make controls visible property true/false server side. The OP asked for javascript to show/hide controls on the page client-side. The only reason to show/hide controls with javascript is because a postback serverside is a lot of network and server resources re-constructing a whole page just to hide/show one or two controls. So, as we were discussing client-side code an asp:Button was not necessary.

Thanks for replying, but my problem is I have a dropdownlist, when i select 'others' ..... a textbox should appear..since i want to save to the back end.. so i need asp.net button...
thanks in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.