In asp.net, the button control first executes js and then executes the background program. asp. netbutton

Source: Internet
Author: User

In asp.net, the button control first executes js and then executes the background program. asp. netbutton

This example describes how to execute js before the background program in asp.net. Share it with you for your reference. The specific analysis is as follows:

In. net to implement button control and click, and then execute js and then the background program, which does not seem difficult, here we will take a look at the implementation method of the button control in asp.net to execute js first and then the background program, hoping to help you.

With regard to the button Server Control, I have always wanted to reduce it by submitting data to the server. Those checks can be implemented on the client.

This requires javascript, but I found that javascript alone is not enough. The click event of the button Server Control is called "onClick", so javascript cannot use this event. Because the name is duplicated. What I want to achieve is to execute the javascript code of the client first when clicking the button, and then execute the background event.

If the html control is used, this problem does not exist. However, I want to implement this function of the server control, and sometimes the server control is also very useful.
Add a server control button to the aspx page

During page initialization, add a client event to the button Server Control. That is, add a code in the Page_Load () method:
Copy codeThe Code is as follows: if (! IsPostBack)
{
// Add client events to button1
BtnSave. Attributes. Add ("OnClick", "return UserAddVerify ()");
}
UserAddVerify is a function implemented on the js end. It is mainly used to check the data validity.
Copy codeThe Code is as follows: function UserAddVerify (){
Var userName = document. getElementById ("TxtUserName"). value;
Var password = document. getElementById ("TxtUserPassword"). value;
Var repassword = document. getElementById ("TxtUserPasswordConfirm"). value;
Var identity = document. getElementById ("TxtUserIdentity"). value;
Var mobile = document. getElementById ("TxtUserMobile"). value;
Var realName = document. getElementById ("TxtUserRealName"). value;
Var btnSave = document. getElementById ("btnSave ");
Var identityReg =/(^ \ d {15} $) | (^ \ d {18} $) | (^ \ d {17} (\ d | X | x) $ )/;
Var assumereg =/1 [3-8] + \ d {9 }/;
If (userName = "" | userName = null ){
Alert ("user name cannot be blank ");
Return false;
}
Else if (password = "" | password = null ){
Alert ("the password cannot be blank ");
Return false;
}
Else if (repassword = "" | repassword = null | repassword! = Password ){
Alert ("sorry, the two passwords are different ");
Return false;
}
Else if (identity = "" | identity = null | identityReg. test (identity) = false ){
Alert ("enter a valid ID card number ");
Return false;
}
Else if (mobile = "" | mobile = null | assumereg. test (mobile) = false ){
Alert ("enter a valid mobile phone number ");
Return false;
}
Else if (realName = "" | realName = null ){
Alert ("name cannot be blank ");
Return false;
}
Return true;
}
The above return true and false are very important, which determines whether to execute the data. The next execution should be to submit the data to the background to process the data. When true is returned, the background executes the button#click method (event ).

I hope this article will help you design your asp.net program.

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.