Asp. NET OnClientClick and onclick events

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.