OnClick vs OnClientClick for an asp:CheckBox?

186,768

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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
Share:
186,768

Related videos on Youtube

Stobor
Author by

Stobor

Updated on January 12, 2021

Comments

  • Stobor
    Stobor over 3 years

    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?

    For example, this works:

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

    and this doesn't (no error):

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

    but this works:

    <asp:Button runat="server" OnClientClick="alert('Hi');" />
    

    and this doesn't (compile time error):

    <asp:Button runat="server" OnClick="alert('hi');" />
    

    (I know what Button.OnClick is for; I'm wondering why CheckBox doesn't work the same way...)

  • Stobor
    Stobor almost 15 years
    Good call. I just checked the generated html, and it's actually putting attributes it doesn't understand into a span around the checkbox input, but otherwise you're right...
  • SolutionYogi
    SolutionYogi almost 15 years
    That's another thing, I don't like the fact that asp:checkbox renders an additional span unnecessarily.
  • Stobor
    Stobor almost 15 years
    Well, some of them. It puts onclick into the input itself, but onClientClick into the span. Weird!
  • SolutionYogi
    SolutionYogi almost 15 years
    Wow. It's been while since I used web controls so my memory is fading. I prefer to use HTML controls as I want to control how my rendered HTML looks like.
  • Stobor
    Stobor over 13 years
    Interesting... That was a project I took over for a while, and it had too many warnings to see anything useful in there... If I remember correctly, the downside to using the HTML control is that you can't do server-side manipulations with it...
  • Joy
    Joy over 11 years
    but what if I want to make that event handler on the client side ?
  • Sergio Rosas
    Sergio Rosas almost 11 years
    That documentation is actually from .Net 1.1. As far as I know, ASP .Net hasn't an OnClick server-side event for CheckBox, so when you write OnClick or OnClientClick, because it doesn't know it, it renders exactly what you wrote, as it's its default behaviour (if I'm not mistaken)
  • Mahdi jokar
    Mahdi jokar almost 8 years
    Seconf function } is missing
  • Protector one
    Protector one over 6 years
    That is the server-side event, not the client-side event; which does not exist.