asp.net in the button control to perform the first JS and then the background program method _ Practical Skills

Source: Internet
Author: User

The example of this article describes the method of button control in ASP.net to execute JS before the background program. Share to everyone for your reference. The specific analysis is as follows:

In. NET to implement button control Click after the first JS and then run the background program This looks not difficult, here we will take a look at a asp.net button control to execute JS before the implementation of the background program implementation method, hope to help you.

About the button this server control, I always wanted to reduce it to submit data to the server. Those detection, or on the client to achieve the better.

This requires JavaScript, but I find that only JavaScript is not enough. The button server control's Click event is called OnClick, so JavaScript cannot use this event. Because of the duplicate name. What I want to do is click the button, execute the client's JavaScript code first, and then perform the background event.

If you are using an HTML control, there is no such problem. However, I just want to implement this function of server control, sometimes server control is also very useful.
Add a Server control button to the ASPX page first

When the page is initialized, add a client event to the button's server control. That is, in the Page_Load () method, add a sentence code:

Copy Code code as follows:
if (! IsPostBack)
{
Adding client events to Button1
BTNSAVE.ATTRIBUTES.ADD ("OnClick", "Return Useraddverify ()");
}

Useraddverify is a function of the JS end, which is mainly used to detect the validity of the data.
Copy Code code 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 mobilereg =/1[3-8]+\d{9}/;
if (UserName = = "" | | UserName = = null) {
Alert ("User name cannot be empty");
return false;
}
else if (password = "" | | Password = = null) {
Alert ("Password cannot be blank");
return false;
}
else if (Repassword = "" | | Repassword = = NULL | | Repassword!= password) {
Alert ("Sorry, two input passwords are not the same");
return false;
}
else if (identity = = "" | | Identity = = NULL | | Identityreg.test (identity) = = False) {
Alert ("Please enter a valid ID number");
return false;
}
else if (mobile = "" | | Mobile = NULL | | Mobilereg.test (MOBILE) = = False) {
Alert ("Please 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 ture and false are very important, which determines whether to go down, execution should be to submit data to the background processing data. When True is returned, the background executes button1_click this method (event).

I hope this article will help you with the ASP.net program design.

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.