OnClick vs OnClientClick for an asp:CheckBox?

后端 未结 8 1377
我在风中等你
我在风中等你 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:58

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

    
    

    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.NET doesn't modify the OnClick attribute and renders it as is on the browser. It would be rendered as:

      
    

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

    And in this scenario

    
    

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

    
    

    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.

提交回复
热议问题