client side script with server side controls

Discussion in 'Javascript' started by Cathryn Lindner, Oct 24, 2003.

  1. I have a web form that has a checkbox on it and a textbox associated
    with it. I want the textbox to be displayed when the checkbox is checked
    and I want this to happen on the client-side instead of posting back to
    the server. The checkbox is a server control (asp:checkbox...). I can
    write some codebehind that will display an alert message upon checking
    the box that looks something like this...

    Page_Load...

    Dim s As String
    Dim scriptString As New System.Text.StringBuilder

    chkExpDate.Attributes.Add("onclick", "alertmsg()")
    scriptString.Append("<script language=JavaScript> function alertmsg()
    {")
    scriptString.Append("alert('hi'); }<")
    scriptstring.Append("/" + "script>"
    s = scriptstring.ToString()

    Page.RegisterStartupScript("startup", s)

    End Sub

    I used this as a test to make sure the server control was running the
    script. But, when I change the script to assess the value of the server
    control checkbox such as checked = true, I get errors and if I just try
    to display the asp:textbox, it doesn't work either.

    Any suggestions?



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Cathryn Lindner, Oct 24, 2003
    #1
    1. Advertising

  2. Cathryn Lindner wrote:

    > I have a web form that has a checkbox on it and a textbox associated
    > with it. I want the textbox to be displayed when the checkbox is checked
    > and I want this to happen on the client-side instead of posting back to
    > the server.


    <script type="text/javascript">
    <!--
    function showHide(
    /** @argument object */ o,
    /** @argument boolean */ b)
    /**
    * @author (C) 2003 Thomas Lahn &lt;&gt;
    * @argdescr o Object to show/hide.
    * @argdescr b <code>true</code> shows the object,
    * <code>false</code> or this argument
    * left out hides it. CSS scripting
    * support is required for this to work.
    */
    {
    if (o
    && typeof o.style != "undefined"
    && typeof o.style.visibility != "undefined")
    o.style.visibility = b ? "visible" : "hidden";
    }
    //-->
    </script>
    <form ...>
    ...
    <input
    type="checkbox"
    checked
    ...
    onclick="showHide(this.form.elements['foobar'], this.checked);"
    ...
    <input
    name="foobar"
    ...>
    </form>

    > The checkbox is a server control (asp:checkbox...).


    You probably do not need it.

    > I can write some codebehind that will display an alert message upon
    > checking the box that looks something like this...
    >
    > Page_Load...
    >
    > Dim s As String
    > Dim scriptString As New System.Text.StringBuilder
    >
    > chkExpDate.Attributes.Add("onclick", "alertmsg()")
    > scriptString.Append("<script language=JavaScript> function alertmsg()
    > {")
    > scriptString.Append("alert('hi'); }<")
    > scriptstring.Append("/" + "script>"
    > s = scriptstring.ToString()
    >
    > Page.RegisterStartupScript("startup", s)
    >
    > End Sub


    That is VBScript, supported only by IE when client-side, and even not
    required here.

    > I used this as a test to make sure the server control was running the
    > script.


    Not the server control is running the JavaScript, the client's
    JavaScript engine is. What you (are trying to) do here is merely
    telling the server to include that script into the code sent to
    the client.


    PointedEars
    Thomas 'PointedEars' Lahn, Nov 23, 2003
    #2
    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. Matt
    Replies:
    14
    Views:
    4,070
    Chad Z. Hower aka Kudzu
    Jan 30, 2004
  2. Mong
    Replies:
    5
    Views:
    4,702
    Kevin Spencer
    May 7, 2004
  3. Magnus Blomberg
    Replies:
    3
    Views:
    697
    Magnus Blomberg
    Apr 14, 2005
  4. Boss302
    Replies:
    0
    Views:
    1,018
    Boss302
    Nov 21, 2006
  5. Zoe Hart
    Replies:
    1
    Views:
    344
    Scott Wisniewski
    Jan 8, 2004
Loading...

Share This Page