OnClientClick is a client-side event method. JavaScript is generally used for processing. That is, run directly on the IE side. Run with a single click.
The onclick event is a server-side event-handling method that runs on the server side, that is, IIS. After clicking the button, execute the postback, then run.
If we have the client's OnClientClick method and the OnClick event processing method on a button, how can we run it according to the normal logic?
We used to do some client-side testing in OnClientClick. Of course, the server can do the same test, but the cost is to interact with the server and consume resources.
Cases:
Test () is a JavaScript function.
<script type= text/javascript " > function test () { var value = document.getElementById ( <%=t1. Clientid%> ). Value; if (value = = cannot be null " ); return false ; }} </script>
T1 as a text box to determine if the input is empty
<asp:textbox id="T1" runat="server"></asp:textbox >
<asp:button id="Button1" runat="server" text= " Submit " onclientclick="return Test ()" onclick="button1_click " />
It is important to note that when we hit this button, the client is executed automatically, and then the server side is executed. If the client returns false, the server-side corresponding method will never execute. This will achieve detection, only through the implementation of the server-side method.
That is to say, if we write:
<asp:button id="Button1" runat="server" text= " Submit " onclientclick="Test (); return false" onclick="button1_click " />
So whatever the result of test () does. The Button1_Click method that corresponds to the server side is never executed. Because return false causes the client to always return false
If we write:
<asp:button id="Button1" runat="server" text= " Submit " onclientclick="Test ()" onclick="button1_click" />
Then there will be button1_click. It's not going to work. That means you don't have to go through the test to execute the server.
We can also trigger client events by BUTTON1.ATTRIBUTES.ADD ("onclick", "Test ()");
Transfer from http://blog.163.com/huang_qy/blog/static/615601452012101535413943/
Asp. NET OnClientClick and onclick events