Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClick vs OnClientClick for an asp:CheckBox?

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...)

like image 496
Stobor Avatar asked Jul 16 '09 02:07

Stobor


People also ask

Does OnClick work on CheckBox?

The CheckBox has been assigned a JavaScript OnClick event handler. When the CheckBox is clicked, the ShowHideDiv JavaScript function is executed. Inside this function, based on whether CheckBox is checked (selected) or unchecked (unselected), the HTML DIV with TextBox is shown or hidden.

What is the difference between OnClick and OnClientClick in asp net?

OnClick will work on server side , OnClientClick will execute on client side before control passed to server. If the client side code returns TRUE then it will go to server. Generally programmers use onClientClick to validate the controls like textbox,etc.

Can we use OnClick and OnClientClick together in asp net?

OnClientClick allows you to add client side OnClick script. In this case, the JavaScript will disable the button element and change its text value to a progress message. When the postback completes, the newly rendered page will revert the button back its initial state without any additional work.

What is CheckBox control in asp net?

It is used to get multiple inputs from the user. It allows user to select choices from the set of choices. It takes user input in yes or no format. It is useful when we want multiple choices from the user. To create CheckBox we can drag it from the toolbox in visual studio.


2 Answers

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.

like image 102
SolutionYogi Avatar answered Sep 24 '22 17:09

SolutionYogi


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.

like image 33
datacop Avatar answered Sep 25 '22 17:09

datacop