"Go" ASP. NET OnClientClick and onclick event "resolved" to identify the user in the dialog box to select Yes or no question ""

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.

1<script type= "Text/javascript" >2         functionTest ()3        {4             varValue = document.getElementById ("%=t1. Clientid%> "). Value;5             if(Value = = "") 6             {7Alert ("cannot be null");8                 return false;9             }Ten         } One</script>

T1 as a text box to determine if the input is empty

<ID= "T1"  runat= "Server"></Asp:textbox  >
<ID= "Button1"  runat= "Server"  Text= "Commit"   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:

<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:

<ID= "Button1"  runat= "Server"  Text= "Commit"   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.

Other than that:

We can also trigger a client-side event by BUTTON1.ATTRIBUTES.ADD ("onclick", "Test ()").

Above from: http://blog.163.com/huang_qy/blog/static/615601452012101535413943/

======================================================================================

Similar article: http://blog.csdn.net/goodshot/article/details/8645740

OnClick and OnClientClick for the rookie I really took a little time to figure it out. The OnClientClick is run on the client side. The onclick is postback on the server side (this is nonsense). But there are execution times and conditions here. Time sequence: Click on a button on the page the first trigger is the client's OnClientClick, and then the onclick will execute. condition: To return true in OnClientClick, the onclick will be followed. In the default case of the Web page: After OnClientClick executes the function, if the function does not return a value OnClientClick returns True, so the onclick can be executed successfully . But there is also the value of not letting the onclick execute at some point . For example, use JavaScript for a user's first level of validation or basic inspection work. If the condition is not met, the onclick is not triggered to return to the server-side continuation logic.

in this case, it is necessary to artificially turn the return of OnClientClick into false. The practice is simple. Defines a JavaScript or VBScript function that returns a value of TRUE or false based on the detection condition. It is then called in Onclientclient. The method is onclientclick= "return fun (really your definition of JavaScript or VBScript function);" This allows the onclick to be triggered if the condition is met, and the onclick is not triggered by a non-conforming condition.

For LinkButton we did an experiment and the test was successful:

<script type= "Text/javascript" >
function Delete_user (obj)
{

var r=confirm ("Confirm Delete this user?") ");
return R;

}

</script>

.......

<td><asp:linkbutton id= "Linkbuttondeleteuser" runat= "Server" Commandname= "Order" commandargument= ' <%# Eval ("UserName")%> ' oncommand= "Linkbuttondeleteuser_command" onclientclick= "return Delete_user (this);" > Delete user </asp:LinkButton></td>

.......

This action is when the user in the page point to the "Delete user" link button, the first pop "confirm delete this user?" The message box, if the user chooses to be sure, to perform the delete user action.

Learning materials:

http://msdn.microsoft.com/zh-cn/library/dd410060

Here is a detailed description of how ASP. NET provides two ways to implement client-side functionality.

Http://www.w3school.com.cn/aspnet/aspnet_refwebcontrols.asp

This includes properties and how to use all Web server controls in ASP.

======================================================================================

Articles with similar questions:

Http://www.cnblogs.com/BensonHe/articles/1780987.html

server controls Magical OnClientClick event blocking callbacks

Many times, we need to validate the input of the server control, If you verify in the background. CS page, the page will be refreshed and not cost-effective in terms of user experience and efficiency, but it is entirely possible that the client will be authenticated using JavaScript, as long as the client event OnClientClick is written. OnClientClick will be triggered before the onclick event. If the OnClientClick event function returns False, then the control is no longer returned, that is, the OnClick event will no longer execute. See the following example:

<script type= "Text/javascript" >

function Check () {

var input = document.getElementById ("Text1"). Value;

if (input = = NULL | | input = = "")

{

Alert ("text box cannot be empty!") ");

return false;

}

return true;

}

</script>

<div>

<asp:textbox id= "Text1" runat= "Server" ></asp:TextBox>

<asp:button id= "summitbtn" runat= "Server" text= "Submit" onclick= "Summitbtn_click" onclientclick= "return check ();"/ >

</div>

Because the check () function has a return value, the preceding return keyword cannot be omitted. If the test passes, which returns True, Summitbtn_click continues execution and can enter database operations on the form's data in Summitbtn_click.

======================================================================================

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.