The ASP. NET Server control button first executes JS and then executes the background method

Source: Internet
Author: User

About button This server control, I always wanted to reduce it to submit data to the server. Those tests, or the client implementation.
This requires JavaScript, but I find it is not enough to have JavaScript alone. The Click event for the button server control is called "OnClick",
So JavaScript can't use this event. Because of the duplicate name. What I want to do is to click the button and execute the client's JavaScript code before performing the background event.

If you are using an HTML control, there is no such problem. However, I just want to implement this feature of server controls, and sometimes server controls are very useful.

Add a Server control button to the ASPX page first

?
1 </asp:button>


Add a client event to the button server control when the page is initialized. This means adding a code to the Page_Load () method:

?
12345 if (! IsPostBack)               {                   // Add a client event to button1                   btnsave.attributes.add ( " OnClick " " return useraddverify () " &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }


Useraddverify is a function of the JS end, which is mainly used to detect the validity of data.

?
1234567891011121314151617181920212223242526272829303132333435363738 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("用户名不能为空");        return false;    }    else if (password == "" || password == null) {        alert("密码不能为空");        return false;    }    else if (repassword == "" || repassword == null || repassword != password) {        alert("对不起,两次输入密码不一样");        return false;    }    else if (identity == "" || identity == null || identityReg.test(identity) === false) {        alert("请输入合法的身份证号码");        return false;    }    else if (mobile == "" || mobile == null || mobileReg.test(mobile) == false) {        alert("请输入合法的手机号码");        return false;    }    else if (realName == "" || realName == null) {        alert("姓名不能为空");        return false;    }    return true;}


The above return ture and false are important, which determines whether or not to go down, execution should be to submit data to the background processing data. When True is returned, the background executes the Button1_Click method (event).

The ASP. NET Server control button first executes JS and then executes the background method

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.