Asp. NET OnClientClick (return) vs. 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 = = "") {
alert ("cannot be null value");
return False
}< BR style= "margin:0px; padding:0px; " >}
</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= "commit"  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 ()");

Asp. NET OnClientClick (return) vs. 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.