asp.net中有时候我们在做用户注册或是修改用户密码时,会遇到有时候用户名重复或是要验证一下用户的原始密码是否正确。这时候我们就要用到CustomValidator这个控件了。但是我们在它自代的ServerValidate方法中写代码后会发现,它根本就没有运行。我是在2003系统下使用的。一直就没有好用过。所以就上网去了一些方法。下面我把我找到的自己使用成功的方法放上来
在<head></head>中加入如下代码
这里说明一下,CheckUpdateAdmin.aspx是用来处理查询条件代码的。在这个页面中你可以设置相应的条件。我把我写的代码放在了后面。
function ValidationFunctionName(source, arguments)
{
var userName =document.getElementById("txtUserName");
var userpwd = document.getElementById("oldPwd")
var oBao = new ActiveXObject("Microsoft.XMLHTTP");
//alert(userName.value);
//alert(userpwd.value);
oBao.open("Get","CheckUpdateAdmin.aspx?username=" + userName.value+"&userpwd="+userpwd.value+"",false);
oBao.send();
var strResult = oBao.responseText;
//如果返回 1 就代表可以使用,否则不能通过验证
if (Number(strResult) == 1)
arguments.IsValid = true;
else
arguments.IsValid = false;
}
</ script >
下面是相应控件代码
< td width = " 29% " bgColor = " #f0f0f0 " height = " 22 " >
< div align = " right " > 管理员昵称: </ div >
</ td >
< td width = " 71% " bgColor = " #f0f0f0 " >< FONT face = " 宋体 " >
< asp:TextBox id = " txtUserName " runat = " server " Enabled = " False " ></ asp:TextBox ></ FONT ></ td >
</ tr >
< tr >
< td width = " 29% " bgColor = " #f0f0f0 " height = " 22 " >
< div align = " right " > 原始密码: </ div >
</ td >
< td width = " 71% " bgColor = " #f0f0f0 " >< FONT face = " 宋体 " >< asp:textbox id = " oldPwd " runat = " server " ></ asp:textbox >< asp:requiredfieldvalidator id = " Requiredfieldvalidator1 " runat = " server " ErrorMessage = " 密码不能空! " ControlToValidate = " txtEnBigClass "
Display = " Dynamic " ></ asp:requiredfieldvalidator >
< asp:CustomValidator id = " CustomValidator1 " runat = " server " ErrorMessage = " 对不起,你的原始密码不对! " ControlToValidate = " oldPwd "
ClientValidationFunction = " ValidationFunctionName " ></ asp:CustomValidator ></ FONT ></ td >
</ tr >
{
// 在此处放置用户代码以初始化页面
Response.Clear();
//string username = Request.QueryString["username"].ToString();
//string userpwd = Request.QueryString["userpwd"].ToString();
if(bc.isnoAdminOldPwd(Request.QueryString["username"].ToString(),Request.QueryString["userpwd"].ToString())==true)
{
Response.Write("0");
}
else
{
Response.Write("1");
}
Response.End();
}
这里有一点要说明的事,在传输Request.QueryString["username"]时,如果传的是中文会出现乱码,无法进行判断,这时你就要在web.config中下面这里,改成gb2312.就可以了。
requestEncoding = " gb2312 "
responseEncoding = " gb2312 "
/>
/// 用来判断用户在修改密码的时候原始密码是否正确
/// </summary>
/// <param name="admin_name">用户的昵称</param>
/// <param name="admin_pwd">用户的原始密码</param>
/// <returns></returns>
public bool isnoAdminOldPwd( string admin_name, string admin_pwd)
{
bool sve = false;
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
conn.Open();
SqlCommand cmd = new SqlCommand("",conn);
cmd.CommandText = "select count(*) from C_admin where ltrim(rtrim(admin_name))='"+admin_name.Trim()+"' and ltrim(rtrim(admin_pwd))='"+admin_pwd.Trim()+"'";
int i = Convert.ToInt32(cmd.ExecuteScalar());
if(i>0)
{
sve = false;
}
else
{
sve = true;
}
return sve;
}
<script language="javascript" type="text/javascript"> function ValidationFunctionName(source, arguments) { var userName =document.getElementById("txtUserName"); var userpwd = document.getElementById("oldPwd") var oBao = new ActiveXObject("Microsoft.XMLHTTP"); //alert(userName.value); //alert(userpwd.value); oBao.open("Get","CheckUpdateAdmin.aspx?username=" + userName.value+"&userpwd="+userpwd.value+"",false); oBao.send(); var strResult = oBao.responseText; //如果返回 1 就代表可以使用,否则不能通过验证 if (Number(strResult) == 1) arguments.IsValid = true; else arguments.IsValid = false; } </script> <script language="javascript" type="text/javascript"> function ValidationFunctionName(source, arguments) { var userName =document.getElementById("txtUserName"); var userpwd = document.getElementById("oldPwd") var oBao = new ActiveXObject("Microsoft.XMLHTTP"); //alert(userName.value); //alert(userpwd.value); oBao.open("Get","CheckUpdateAdmin.aspx?username=" + userName.value+"&userpwd="+userpwd.value+"",false); oBao.send(); var strResult = oBao.responseText; //如果返回 1 就代表可以使用,否则不能通过验证 if (Number(strResult) == 1) arguments.IsValid = true; else arguments.IsValid = false; } </script> <script language="javascript" type="text/javascript"> function ValidationFunctionName(source, arguments) { var userName =document.getElementById("txtUserName"); var userpwd = document.getElementById("oldPwd") var oBao = new ActiveXObject("Microsoft.XMLHTTP"); //alert(userName.value); //alert(userpwd.value); oBao.open("Get","CheckUpdateAdmin.aspx?username=" + userName.value+"&userpwd="+userpwd.value+"",false); oBao.send(); var strResult = oBao.responseText; //如果返回 1 就代表可以使用,否则不能通过验证 if (Number(strResult) == 1) arguments.IsValid = true; else arguments.IsValid = false; } </script>