JS in OnClientClick How to control the execution of onclick _javascript skills

Source: Internet
Author: User

OnClientClick is a client-side script, typically using JavaScript, running on the client, ie, and immediately after clicking.

OnClick is a server-side event handler, using C # or vb.net, on the server side, which is IIS, click the button, execute the postback, and then run.

If you want to call the OnClientClick before you call the OnClick event, you should return false in the OnClientClick event. If you do not return false, the OnClick event will continue to be invoked after the OnClientClick is executed!

The implementation method is as follows:

<asp:button id= "Btn_save" runat= "Server" text= "save" cssclass= "Button_bak" onclientclick= "return Whetherempty ();" onclick= "Btn_save_click"/>
//javascript implemented as follows: <script language= "
JavaScript" type= "Text/javascript" >
function whetherempty ()
{
//alert ("KPI information cannot be all empty!") ");
Emptflag = false;
var kpiname = document.getElementById ("<%=txt_KPIName.ClientID%>"). Value;
var jobgoal = document.getElementById ("<%=txt_JobGoal.ClientID%>"). Value;
var weight = document.getElementById ("<%=txt_Weight.ClientID%>"). Value;
var standard = document.getElementById ("<%=txt_Standard.ClientID%>"). Value;
if (Kpiname = = "" && jobgoal = = "" && weight = "" && standard = = "")
{
Emptflag = true; c14/>}
if (Emptflag)
{
alert ("KPI information cannot be all empty!") ");
return false;
}
}
</script>

If onclientclick= "return Whetherempty ()" No return, in JavaScript even return Fales,onclick will still execute!

Here to introduce the difference between OnClientClick and onclick

In fact, sometimes need the client and service side double check, why do you say so? than the fruit said, a user name of the text box, in the client we pass JS Checksum, can only enter letters and numbers, can not have special characters! Usually there is no problem, but there is always a shame to lose the way to "sabotage", this time need to double check! To put it bluntly, the server and the client are all in check!

For example: Button has: OnClick event and OnClientClick properties, the former is generally the service side of the Click event! The latter is the client click event!

Let's do a test! Add the following code to the Default.aspx page!

<script language= "javascript" type= "Text/javascript" >
function ButtonClick () {
alert ("I am a client click event");
return false; 
}

Page code:

<form id= "Form1" runat= "Server" > <div> <asp:button id= "Button1" runat= "
server" text= " Some button is the service-side control, the default use of the OnClientClick event, please disable the browser's JS feature experience "
onclick=" Button1_Click "onclientclick=" Return ButtonClick ( ); "/>
<asp:label id=" Label1 "runat=" Server "text=" "></asp:Label>
</div>

CS File Code:

protected void Button1_Click (object sender, EventArgs e)
{this
. Label1.Text = "I am a service-side click event";
}

Everybody run to see what effect? You may find that only the code that pops up in JS! Yes, that's exactly what we want, by return false to "mask" the server's checksum! What are the benefits? is not to refresh the page Ah!

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.