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
?
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 () "              } |
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