How can I add a client-side event to an asp:CheckBox ?

Discussion in 'ASP .Net' started by mark4asp, Jan 2, 2008.

  1. mark4asp

    mark4asp Guest

    Here is the control:

    <asp:CheckBox ID="chkLite" runat="server" Checked="False"
    AutoPostBack="True" OnCheckedChanged="chkLite_CheckedChanged" />

    Here is the code-behind:

    chkLite.Attributes.Add("onblur", "LiteChange(this);");

    Here is the control as asp.net renders it in the html:

    <span onblur="LiteChange(this);"><input id="_ctl0_cph_chkLite"
    type="checkbox" name="_ctl0:cph:chkLite" checked="checked"
    onclick="javascript:setTimeout('__doPostBack(\'_ctl0$cph$chkLite\',\'\')
    ', 0)" language="javascript" /></span>

    The problem with this is that the onblur event is not fired because the
    parent span does not notice when chkLite is checked or unchecked. The
    javascript LiteChange() function never fires.

    Is there a way I can get this (below) in my html:

    <input id="_ctl0_cph_chkLite" type="checkbox" name="_ctl0:cph:chkLite"
    checked="checked"
    onclick="javascript:setTimeout('__doPostBack(\'_ctl0$cph$chkLite\',\'\')
    ', 0)" onblur="LiteChange(this);" language="javascript" />

    In other words, attach the onblur event directly to the checkbox?

    The javascript is below (it's just there as a demonstration, currently
    functionally inactive, as you can see).

    function LiteChange(oSpan)
    {
    if(oSpan.children[0].checked)
    alert('chkLite is checked');
    else
    alert('chkLite not checked');
    alert('hello');
    }
     
    mark4asp, Jan 2, 2008
    #1
    1. Advertising

  2. Instead of asp:CheckBox use <input type="checkbox" runat="server" ... />.
    Set all client events in their natural way. Set
    onserverchange="chkLite_CheckedChanged".

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "mark4asp" <> wrote in message
    news:017a39b4$0$25652$...
    > Here is the control:
    >
    > <asp:CheckBox ID="chkLite" runat="server" Checked="False"
    > AutoPostBack="True" OnCheckedChanged="chkLite_CheckedChanged" />
    >
    > Here is the code-behind:
    >
    > chkLite.Attributes.Add("onblur", "LiteChange(this);");
    >
    > Here is the control as asp.net renders it in the html:
    >
    > <span onblur="LiteChange(this);"><input id="_ctl0_cph_chkLite"
    > type="checkbox" name="_ctl0:cph:chkLite" checked="checked"
    > onclick="javascript:setTimeout('__doPostBack(\'_ctl0$cph$chkLite\',\'\')
    > ', 0)" language="javascript" /></span>
    >
    > The problem with this is that the onblur event is not fired because the
    > parent span does not notice when chkLite is checked or unchecked. The
    > javascript LiteChange() function never fires.
    >
    > Is there a way I can get this (below) in my html:
    >
    > <input id="_ctl0_cph_chkLite" type="checkbox" name="_ctl0:cph:chkLite"
    > checked="checked"
    > onclick="javascript:setTimeout('__doPostBack(\'_ctl0$cph$chkLite\',\'\')
    > ', 0)" onblur="LiteChange(this);" language="javascript" />
    >
    > In other words, attach the onblur event directly to the checkbox?
    >
    > The javascript is below (it's just there as a demonstration, currently
    > functionally inactive, as you can see).
    >
    > function LiteChange(oSpan)
    > {
    > if(oSpan.children[0].checked)
    > alert('chkLite is checked');
    > else
    > alert('chkLite not checked');
    > alert('hello');
    > }
     
    Eliyahu Goldin, Jan 2, 2008
    #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. Boss302
    Replies:
    0
    Views:
    1,084
    Boss302
    Nov 21, 2006
  2. Xavier Pacheco

    client side checkbox event

    Xavier Pacheco, Jul 27, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    142
    Xavier Pacheco
    Jul 27, 2005
  3. Aymer
    Replies:
    1
    Views:
    401
    Alvin Bruney
    Sep 19, 2003
  4. smita
    Replies:
    3
    Views:
    780
  5. Guest

    Fire server side event before client side event

    Guest, Mar 17, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    622
    Ken Cox [Microsoft MVP]
    Mar 18, 2005
Loading...

Share This Page