How do I show/hide Label/Textbox when a RadioButton is clicked?

Discussion in 'ASP .Net' started by Jason, Sep 13, 2004.

  1. Jason

    Jason Guest

    I am new to WebForms and am far more comfortable with WinForms. I am
    wondering how simple things such as showing/hiding a label and textbox can
    be accomplished if a certain Radiobutton option is clicked/unclicked without
    taking a trip back to the webserver? Is there some easy way to do this? (I'm
    gussing it will require JavaScript)
    Jason, Sep 13, 2004
    #1
    1. Advertising

  2. Javascript is the route to take.

    I would wire to the "onclick" event of the radio button.

    The javascript I use to show and hide controls is:

    document.all["tblShowMe"].style.display = "";
    document.all["tblHideMe"].style.display = "none";

    If you need more info, let me know.

    HTH,

    bill


    tblReplacementInvoice.style.display = "none"
    "Jason" <> wrote in message
    news:...
    > I am new to WebForms and am far more comfortable with WinForms. I am
    > wondering how simple things such as showing/hiding a label and textbox can
    > be accomplished if a certain Radiobutton option is clicked/unclicked

    without
    > taking a trip back to the webserver? Is there some easy way to do this?

    (I'm
    > gussing it will require JavaScript)
    >
    >
    William F. Robertson, Jr., Sep 13, 2004
    #2
    1. Advertising

  3. Jason

    Karl Guest

    Without going back to the server it does require javascript, something
    untested like:


    <asp:label id="usernameLabel" runat="server" /> <asp:dropdownlist id="ddl"
    runat="server" onChange="changed(this);" />


    <script language="javascript">
    function changed(ddl){
    var username = document.getElementById('<%=usernameLabel.UniqueId%>');
    switch (ddl.options[ddl.selectedIndex].value){
    case "1":
    username.style.visibility = 'hidden';
    break;
    default:
    username.style.visibility = 'visible';
    break;
    }
    }

    </script>

    That's just a basic example,
    Karl
    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Jason" <> wrote in message
    news:...
    > I am new to WebForms and am far more comfortable with WinForms. I am
    > wondering how simple things such as showing/hiding a label and textbox can
    > be accomplished if a certain Radiobutton option is clicked/unclicked

    without
    > taking a trip back to the webserver? Is there some easy way to do this?

    (I'm
    > gussing it will require JavaScript)
    >
    >
    Karl, Sep 13, 2004
    #3
  4. Jason

    Jason Guest

    I also discovered that if I use the RadioButtonList, set
    AutoPostBack="True", and hook the SelectedIndexChanged event I can
    hide/unhide labels and textboxes using C# rather than JavaScript.

    However, it is obvious it would be much more efficient to use JavaScript for
    something like this... so I guess I better learn it.

    "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
    message news:...
    > Without going back to the server it does require javascript, something
    > untested like:
    >
    >
    > <asp:label id="usernameLabel" runat="server" /> <asp:dropdownlist id="ddl"
    > runat="server" onChange="changed(this);" />
    >
    >
    > <script language="javascript">
    > function changed(ddl){
    > var username = document.getElementById('<%=usernameLabel.UniqueId%>');
    > switch (ddl.options[ddl.selectedIndex].value){
    > case "1":
    > username.style.visibility = 'hidden';
    > break;
    > default:
    > username.style.visibility = 'visible';
    > break;
    > }
    > }
    >
    > </script>
    >
    > That's just a basic example,
    > Karl
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    Jason, Sep 14, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne  Wengert

    Determine RadioButton Clicked

    Wayne Wengert, Mar 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    367
    Wayne Wengert
    Mar 17, 2005
  2. UJ
    Replies:
    4
    Views:
    46,466
    pamam
    Apr 24, 2009
  3. Barry
    Replies:
    0
    Views:
    236
    Barry
    Jan 24, 2007
  4. ll
    Replies:
    2
    Views:
    633
    Thomas 'PointedEars' Lahn
    May 1, 2008
  5. Knut Krueger
    Replies:
    8
    Views:
    256
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page