OnClick vs OnClientClick for an asp:CheckBox?

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

    I was cleaning up warnings and messages and see that VS does warn about it: Validation (ASP.Net): Attribute 'OnClick' is not a valid attribute of element 'CheckBox'. Use the html input control to specify a client side handler and then you won't get the extra span tag and the two elements.

    0 讨论(0)
  • 2020-12-07 17:03

    Asp.net CheckBox is not support method OnClientClick.
    If you want to add some javascript event to asp:CheckBox you have to add related attributes on "Pre_Render" or on "Page_Load" events in server code:

    C#:

        private void Page_Load(object sender, EventArgs e)
        {
            SomeCheckBoxId.Attributes["onclick"] = "MyJavaScriptMethod(this);";
        }
    

    Note: Ensure you don't set AutoEventWireup="false" in page header.

    VB:

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            SomeCheckBoxId.Attributes("onclick") = "MyJavaScriptMethod(this);"
        End Sub
    
    0 讨论(0)
提交回复
热议问题