Checkbox, AutoPostBack & JavaScript

Discussion in 'ASP .Net Web Controls' started by Chris Walls, Dec 29, 2004.

  1. Chris Walls

    Chris Walls Guest

    I have a CheckBox that when clicked, needs to first invoke a client-side
    JavaScript function, then post back to the server. My ASP.NET code looks
    like the following:

    <asp:CheckBox ID="MyCheckBox" Runat="server" AutoPostBack="True"
    OnCheckedChanged="OnMyCheckBoxCheckedChanged" />

    I normally add JS code to a control by executing the following code in the
    Page_Load method:

    MyCheckBox.Attributes.Add("onclick", "DoSomethingFirst();");

    However, the DoSomethingFirst() JS call is added to the generated SPAN tag
    that surrounds the checkbox control, so my "DoSomethingFirst" function fires
    *after* the AutoPostBack fires. How can I get my JS function to fire
    *before* the AutoPostBack fires?

    Thanks,
    Chris
    Chris Walls, Dec 29, 2004
    #1
    1. Advertising

  2. Hi Chris,

    Thanks for your posting. As for the checkbox post back problem, it is
    caused by the asp.net CheckBox control is encapsulated and we're hard to do
    some additional customize. Currently if you want to add some other
    clientscript before the CheckBox post back, we may have two options:

    1. Manualy create a new checkBox class which derived from the asp.net's
    CheckBox control and override the render method so as to insert our own
    script before the build in "AutoPostBack" script (__doPostBack)

    2. Use the InputCheckBox control in the System.Web.UI.HtmlControls
    namespace and we can add a serverchange event for it and also handler its
    clientside "onclick" script event. But we need to manually post back the
    page when user click it at clientside, for example:

    <input type="checkbox" id="chkClient" runat="server" value="..."
    name="chkClient" onclick="alert('hello');document.forms[0].submit();"
    onserverchange="chkServer_CheckedChanged">

    in page's codebehind we have :

    protected void chkServer_CheckedChanged(object sender, System.EventArgs e)
    {
    Response.Write("<br>CheckChanged at: " + DateTime.UtcNow.ToString());
    }


    Hope this helps. Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], Dec 30, 2004
    #2
    1. Advertising

  3. Chris Walls

    Chris Walls Guest

    The second option worked just fine. Thanks for the help.

    "Steven Cheng[MSFT]" <> wrote in message
    news:1FZz$...
    > Hi Chris,
    >
    > Thanks for your posting. As for the checkbox post back problem, it is
    > caused by the asp.net CheckBox control is encapsulated and we're hard to
    > do
    > some additional customize. Currently if you want to add some other
    > clientscript before the CheckBox post back, we may have two options:
    >
    > 1. Manualy create a new checkBox class which derived from the asp.net's
    > CheckBox control and override the render method so as to insert our own
    > script before the build in "AutoPostBack" script (__doPostBack)
    >
    > 2. Use the InputCheckBox control in the System.Web.UI.HtmlControls
    > namespace and we can add a serverchange event for it and also handler its
    > clientside "onclick" script event. But we need to manually post back the
    > page when user click it at clientside, for example:
    >
    > <input type="checkbox" id="chkClient" runat="server" value="..."
    > name="chkClient" onclick="alert('hello');document.forms[0].submit();"
    > onserverchange="chkServer_CheckedChanged">
    >
    > in page's codebehind we have :
    >
    > protected void chkServer_CheckedChanged(object sender, System.EventArgs e)
    > {
    > Response.Write("<br>CheckChanged at: " + DateTime.UtcNow.ToString());
    > }
    >
    >
    > Hope this helps. Thanks.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
    >
    Chris Walls, Dec 30, 2004
    #3
  4. You're Welcome Chris,

    HAPPY HOLIDAYS.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], Dec 31, 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. Susan van Houen

    autopostback - javascript intercept and cancel

    Susan van Houen, Jul 17, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,599
    Marina
    Jul 17, 2003
  2. Replies:
    1
    Views:
    5,570
    =?Utf-8?B?TXVoYW1tYWQgTW9zYQ==?=
    Aug 16, 2006
  3. CheckBox AutoPostBack=True

    , Dec 3, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    757
  4. BDRichardson

    CheckBox State And AutoPostBack

    BDRichardson, Nov 17, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    157
    BDRichardson
    Nov 17, 2006
  5. Michael Champagne
    Replies:
    4
    Views:
    332
    Matt Kruse
    Apr 20, 2004
Loading...

Share This Page