Differences between onclientclick and onclick in ASP. NET

Source: Internet
Author: User

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.

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.