OnClick vs OnClientClick for an asp:CheckBox?

后端 未结 8 1375
我在风中等你
我在风中等你 2020-12-07 16:33

Does anyone know why a client-side javascript handler for asp:CheckBox needs to be an OnClick=\"\" attribute rather than an OnClientClick=\"\" attribute, as for asp:Button?<

相关标签:
8条回答
  • 2020-12-07 16:41

    You are right this is inconsistent. What is happening is that CheckBox doesn't HAVE an server-side OnClick event, so your markup gets rendered to the browser. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox_events.aspx

    Whereas Button does have a OnClick - so ASP.NET expects a reference to an event in your OnClick markup.

    0 讨论(0)
  • 2020-12-07 16:41

    One solution is with JQuery:

    $(document).ready(
        function () {
            $('#mycheckboxId').click(function () {
                   // here the action or function to call
            });
        }
    );
    
    0 讨论(0)
  • 2020-12-07 16:42

    For those of you who got here looking for the server-side OnClick handler it is OnCheckedChanged

    0 讨论(0)
  • 2020-12-07 16:53

    Because they are two different kinds of controls...

    You see, your web browser doesn't know about server side programming. it only knows about it's own DOM and the event models that it uses... And for click events of objects rendered to it. You should examine the final markup that is actually sent to the browser from ASP.Net to see the differences your self.

    <asp:CheckBox runat="server" OnClick="alert(this.checked);" />
    

    renders to

    <input type="check" OnClick="alert(this.checked);" />
    

    and

    <asp:CheckBox runat="server" OnClientClick="alert(this.checked);" />
    

    renders to

    <input type="check" OnClientClick="alert(this.checked);" />
    

    Now, as near as i can recall, there are no browsers anywhere that support the "OnClientClick" event in their DOM...

    When in doubt, always view the source of the output as it is sent to the browser... there's a whole world of debug information that you can see.

    0 讨论(0)
  • 2020-12-07 16:58

    That is very weird. I checked the CheckBox documentation page which reads

    <asp:CheckBox id="CheckBox1" 
         AutoPostBack="True|False"
         Text="Label"
         TextAlign="Right|Left"
         Checked="True|False"
         OnCheckedChanged="OnCheckedChangedMethod"
         runat="server"/>
    

    As you can see, there is no OnClick or OnClientClick attributes defined.

    Keeping this in mind, I think this is what is happening.

    When you do this,

    <asp:CheckBox runat="server" OnClick="alert(this.checked);" />
    

    ASP.NET doesn't modify the OnClick attribute and renders it as is on the browser. It would be rendered as:

      <input type="checkbox" OnClick="alert(this.checked);" />
    

    Obviously, a browser can understand 'OnClick' and puts an alert.

    And in this scenario

    <asp:CheckBox runat="server" OnClientClick="alert(this.checked);" />
    

    Again, ASP.NET won't change the OnClientClick attribute and will render it as

    <input type="checkbox" OnClientClick="alert(this.checked);" />
    

    As browser won't understand OnClientClick nothing will happen. It also won't raise any error as it is just another attribute.

    You can confirm above by looking at the rendered HTML.

    And yes, this is not intuitive at all.

    0 讨论(0)
  • 2020-12-07 16:59

    You can do the tag like this:

    <asp:CheckBox runat="server" ID="ckRouteNow" Text="Send Now" OnClick="checkchanged(this)" />
    

    The .checked property in the called JavaScript will be correct...the current state of the checkbox:

      function checkchanged(obj) {
          alert(obj.checked)
      }
    
    0 讨论(0)
提交回复
热议问题