In web development, the onclientclick and onclick events of controls are often used.
If you do not use it properly, you will often encounter unexpected things that make you confused. Here are some of my summary:
First, we need to understand these two events:
Onclientclick is a client-side event method. Generally, JavaScript is used for processing. That is, it runs directly on the IE side.
Onclick event is a server-side event processing method. It is run on the server side, that is, IIS. click the button to execute PostBack and then run.
If we have both the client's onclientclick method and the onclick event processing method on a button, how can we operate according to the normal logic.
In onclientclick, we often perform client detection. of course, you can perform the same check on the server, but the price for doing so is to interact with the server, consume resources, and the user experience is poor...
Example:
// Jsdel () is a JavaScript function.
<Asp: button id = "btndel" width = "80px" cssclass = "buttonstyle" runat = "server" text = "delete" onclientclick = "Return jsdel (); "onclick =" btndel_click1 "/>
It should be noted that when we click this button, the client automatically runs first and then the server. if the client returns false, the corresponding method on the server will never be executed. in this way, the server-side method is executed only when the detection succeeds.
That is to say, if we write:
<Asp: button id = "btndel" width = "80px" cssclass = "buttonstyle" runat = "server" text = "delete" onclientclick = "jsdel (); Return false; "onclick =" btndel_click1 "/>
No matter how jsdel is executed, the btndel_click1 method corresponding to the server will never be executed. Because return false makes the Client Always return false.
If we write:
<Asp: button id = "btndel" width = "80px" cssclass = "buttonstyle" runat = "server" text = "delete" onclientclick = "jsdel (); "onclick =" btndel_click1 "/>
Then btndel_click1 will happen, and it will not be able to achieve the same effect. That is to say, if you do not pass the detection, you will also execute the server method.
So you must pay attention to it.
Of course, we can also use the BTN. Attribute. Add () method to perform similar processing.