asp:checkbox hide/show text/fields when it's clicked without doing a postback?

Discussion in 'ASP .Net' started by UJ, Apr 26, 2006.

  1. UJ

    UJ Guest

    Is there a way with a asp:checkbox to run a JavaScript to display/hide
    text/input on the screen without doing a postback?

    I also need to be able to access the stuff at the server so I need to have
    run=server with it.

    TIA - Jeff.
    UJ, Apr 26, 2006
    #1
    1. Advertising

  2. Yes you can add javascript to the control.

    in your code behind you can do this through ...

    Checkbox.Attributes.Add("onclick", "yourjavascriptfunction(this)")

    "UJ" <> wrote in message
    news:...
    > Is there a way with a asp:checkbox to run a JavaScript to display/hide
    > text/input on the screen without doing a postback?
    >
    > I also need to be able to access the stuff at the server so I need to have
    > run=server with it.
    >
    > TIA - Jeff.
    >
    >
    Greg Young [MVP], Apr 26, 2006
    #2
    1. Advertising

  3. UJ

    Onwuka Emeka Guest

    Yes there are ways to do that. the asp:checkbox ultimately renders a an
    <input type='checkbox'> html tag to the web browser .
    if it were a html control you can do this by adding an onclick attibute to
    the tag i.e. <input type='checkbox' onclick='someJavaScript();'> right?
    you can also do that with an asp:CheckBox control by adding the attribute
    key, value pair to the Attributes collection of the control thus:
    CheckBox.Attributes.Add("attributename","attributevalue") .

    copy, save and host the code below. The trick is in the OnInit method where
    the onClick attribute of the checkbox is added.

    <%@ Page language="c#" AutoEventWireup="false" %>
    <HTML>
    <HEAD>
    <title>Javascript Show Hide</title>

    <script language=javascript>
    function showHide(checkbox,textboxId)
    {
    var textbox = document.getElementById(textboxId);
    if(checkbox.checked)
    {
    textbox.style.display = '';
    }
    else
    {
    textbox.style.display = 'none';
    }
    }
    </script>
    <script language=C# runat=server>
    override protected void OnInit(EventArgs e)
    {
    chbShowHide.Attributes.Add("onClick",string.Format("javascript:showHide(this,'{0}');",txtShowHide.ClientID));
    base.OnInit(e);
    }
    </script>
    </HEAD>
    <body >
    <form id="frmShowHide" method="post" runat="server">
    <asp:TextBox id="txtShowHide"
    runat="server"></asp:TextBox>
    <asp:CheckBox id="chbShowHide" Checked=True
    runat="server"></asp:CheckBox>
    </form>
    </body>
    </HTML>



    "UJ" <> wrote in message
    news:...
    > Is there a way with a asp:checkbox to run a JavaScript to display/hide
    > text/input on the screen without doing a postback?
    >
    > I also need to be able to access the stuff at the server so I need to have
    > run=server with it.
    >
    > TIA - Jeff.
    >
    >
    Onwuka Emeka, Apr 26, 2006
    #3
  4. UJ

    asharsyed

    Joined:
    May 20, 2008
    Messages:
    1
    Object Required!!!

    <script type ="text/javascript">
    function DispTextBox(checkbox,textboxId)
    {
    var textbox = document.getElementById(textboxId);
    if(checkbox.checked)
    {
    textbox.style.display = '';
    }
    else
    {
    textbox.style.display = 'none';
    }
    }
    </script>
    <script language="C#" runat="server">
    override protected void OnInit(EventArgs e)
    {
    cb_others.Attributes.Add("onClick", string.Format("javascript:DispTextBox('{0}','{1}');", cb_others.ClientID ,txt_others.ClientID));
    base.OnInit(e);
    }
    </script>


    I wrote the following script to hide or display the text box when the checkbox is checked or unchecked with little modifications.

    But I still get an error 'Object Required' which I couldn't sort out. Please help!
    asharsyed, May 20, 2008
    #4
  5. UJ

    pamam

    Joined:
    Apr 24, 2009
    Messages:
    1
    I found your post and I am having the same problem with the "Object Required" message in a WSS Content page that I am customizing. It seemed to work properly in a standalone asp.net web page as written, but not in WSS.

    Does anyone have any suggestions or advice about where to find any help.
    Thanks,8)

    Mel
    pamam, Apr 24, 2009
    #5
    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. Jason
    Replies:
    3
    Views:
    50,564
    Jason
    Sep 14, 2004
  2. Rob Roberts
    Replies:
    2
    Views:
    15,819
    Rob Roberts
    Apr 11, 2006
  3. =?Utf-8?B?RG90TmV0RGV2?=

    DataGrid and embeded Checkbox..How to find if checkbox clicked

    =?Utf-8?B?RG90TmV0RGV2?=, Oct 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    810
    David Wier
    Oct 6, 2006
  4. Joey Pang
    Replies:
    4
    Views:
    511
    Joey Pang
    Jun 13, 2005
  5. TGEAR

    show or hide fields using a checkbox

    TGEAR, Mar 13, 2007, in forum: Javascript
    Replies:
    1
    Views:
    155
    Bart Lateur
    Mar 13, 2007
Loading...

Share This Page