My opinion on OnClientClick and onclick in asp.net

Source: Internet
Author: User

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.

Related Article

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.