这样用JS怎么不行啊?
我在cs程序里添加了onclick事件:
this.btnEdit.Attributes.Add( "OnClick ", "return MailOperate() ");
然后ascx页面引用:
<script language= "javascript " type= "text/javascript " src= "../../Js/SystemParameter.js "> </script>
SystemParameter.js里的代码为:
function MailOperate()
{
document.getElementById( " <%=lblOldPwd.ClientID %> ").style.display = "block ";
document.getElementById( " <%=lblNewPwd.ClientID %> ").style.display = "block ";
document.getElementById( " <%=lblNewPwdAgain.ClientID %> ").style.display = "block ";
document.getElementById( " <%=txtNewPwd.ClientID %> ").style.display = "block ";
document.getElementById( " <%=txtNewPwdAlagin.ClientID %> ").style.display = "block ";
var newpwd = document.getElementById( " <%=txtNewPwd.ClientID %> ");
var alaginpwd = document.getElementById( " <%=txtNewPwdAlagin.ClientID %> ");
var servername = " <%=txtServerName.ClientID %> ";
var txtServerName = document.getElementById(servername);
var smtpname = " <%=txtSMTPName.ClientID %> ";
var txtSMTPName = document.getElementById(smtpname);
var smtppwd = " <%=txtSMTPPWD.ClientID %> ";
var txtSMTPPWD = document.getElementById(smtppwd);
var sender = " <%=txtSender.ClientID %> ";
var txtSender = document.getElementById(sender);
var edit = " <%=btnEdit.ClientID %> ";
var btnEdit = document.getElementById(edit);
var message = " <%=lblMessage.ClientID %> ";
var lblMessage = document.getElementById(message);
if (txtServerName.style.borderWidth == "0px ")
{
txtServerName.style.borderWidth = 2;
txtServerName.readOnly = " ";
txtSMTPName.style.borderWidth = 2;
txtSMTPName.readOnly = " ";
txtSMTPPWD.style.borderWidth = 2;
//txtSMTPPWD.readOnly = " ";
newpwd.style.borderWidth = 2;
newpwd.readOnly = " ";
alaginpwd.style.borderWidth = 2;
alaginpwd.readOnly = " ";
txtSender.style.borderWidth = 2;
txtSender.readOnly = " ";
lblMessage.innerHTML = " ";
btnEdit.value = "Submit ";
return false;
}
else
{
var regServerName = new RegExp( "^[a-zA-Z]+[.].+[.][a-zA-Z]+$ ");
var regSMTPName = new RegExp( "^[0-9a-zA-Z_]+$ ");
var regMail = new RegExp( "^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+ ");
if (txtServerName.value.replace(/(^\s*)|(\s*$)/g, " ") == " ")
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "Server Name cannot be empty ";
return false;
}
if (txtSMTPName.value.replace(/(^\s*)|(\s*$)/g, " ") == " ")
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "SMTP Name cannot be empty ";
return false;
}
if (txtSMTPPWD.value.replace(/(^\s*)|(\s*$)/g, " ") == " ")
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "SMTP Password cannot be empty ";
return false;
}
if (newpwd.value.replace(/(^\s*)|(\s*$)/g, " ") == " ")
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "SMTP New Password cannot be empty ";
return false;
}
if (alaginpwd.value.replace(/(^\s*)|(\s*$)/g, " ") == " ")
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "Password confirmation cannot be empty ";
return false;
}
if (newpwd.value.replace(/(^\s*)|(\s*$)/g, " ") != alaginpwd.value.replace(/(^\s*)|(\s*$)/g, " "))
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "The new password and the confirmation password must be consistent ";
return false;
}
if (newpwd.value.replace(/(^\s*)|(\s*$)/g, " ") == txtSMTPPWD.value.replace(/(^\s*)|(\s*$)/g, " "))
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "The new password cannot be same with the original password ";
return false;
}
if (txtSender.value.replace(/(^\s*)|(\s*$)/g, " ") == " ")
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "Sender cannot be empty ";
return false;
}
if (!regServerName.test(txtServerName.value))
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "Server name format is wrong ";
return false;
}
if (!regSMTPName.test(txtSMTPName.value))
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "Please input the letter,the numeral and the _ at SMTP name ";
return false;
}
if (!regSMTPName.test(txtSMTPPWD.value))
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "Please input the letter,the numeral and the _ at SMTP password ";
return false;
}
if (!regMail.test(txtSender.value))
{
lblMessage.style.color= "red ";
lblMessage.innerHTML = "Sender format is wrong ";
return false;
}
return;
}
}
这样根本不行啊,我原来没放在JS文件里面,直接就放在ascx页面是可以的,但是移到了JS文件里这样就不行了。
是什么原因啊,哪里有问题啊?
[解决办法]
JS路径问题
因为你的js在ascx控件中引用
如果你的ascx和你的页面不在同一个目录就会有这个问题
[解决办法]
就是 " <%=lblOldPwd.ClientID %> "得到的结果跟 " <%=this.ClientID %> " + "_lblOldPwd "得到的结果是一样的.
如果你有3个lbl控件,分别lbl1,lbl2,lbl3
你可以这样取它们的客户端ID
<script>
var id1 = " <%=lbl1.ClientID %> ";
var id2 = " <%=lbl2.ClientID %> ";
var id3 = " <%=lbl3.ClientID %> ";
</script>
也可以这样取:
<script>
var id = " <%=this.ClientID %> "
// 下面3个id就不要用到 <%=,就可以把下面3个id放到js文件中
var id1 = id + "_lbl1 ";
var id2 = id + "_lbl2 ";;
var id3 = id + "_lbl3 ";;
</script>