In web development, the control's OnClientClick and onclick events are often used.
If used improperly, often encounter some unexpected things happen, let a person feel confused. Here are some of my little ways to sum up:
First we need to understand these two events:
OnClientClick is a client-side event method. JavaScript is generally used for processing. That is, running directly on the IE side. Run with a click.
The onclick event is a server-side event-handling method that runs on the server side, in IIS. Click the button, execute postback, and then run.
If a button on the same time we have the client OnClientClick method and the OnClick event processing method, how to follow the normal logic to run it.
OnClientClick we are often used to do some client testing. Of course, the server can do the same test, but the cost is to interact with the server, consume resources, and the user experience is not good ...
Cases:
Jsdel () is a JavaScript function.
<asp:button id= "Btndel" width= "80px" cssclass= "ButtonStyle" runat= "server" text= "delete" onclientclick= "return Jsdel () ;" onclick= "Btndel_click1"/>
Note that when we hit this button, the client is automatically executed first and then the server side is executed. If the client returns false, then the server-side corresponding method will never be executed. This will achieve detection, only through the implementation of server-side methods.
Which means if we write:
<asp:button id= "Btndel" width= "80px" cssclass= "ButtonStyle" runat= "server" text= "delete" onclientclick= "Jsdel (); return false; onclick= "Btndel_click1"/>
So no matter how the knot Jsdel executes. The server-side corresponding Btndel_click1 method will never be executed. Because return false makes the client always returns false
If we write:
<asp:button id= "Btndel" width= "80px" cssclass= "ButtonStyle" runat= "server" text= "delete" onclientclick= "Jsdel ();" onclick= "Btndel_click1"/>
Then the btndel_click1 will happen. It's also not going to work. That means you don't have to pass the test to do the server's way.
So it must be noted.
Of course we can also pass btn. Attribute.add () method for similar processing.