CustomValidator1
是自定义验证控件
ControlToValidate
选择文本框
ErrorMessage
出错时的显示
Display动态的
Text:*
ValidationSummary
ShowMessageBox :True
ShowSummary:False
private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string userName=args.Value;
if(DB.judge(userName))
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
}
public static bool judge(string userName)
{
SqlConnection con = DB.CreateCon();
con.Open();
SqlCommand cmd = new SqlCommand("select count(*) from login where userName='"+userName+"'",con);
int n = Convert.ToInt32(cmd.ExecuteScalar());
if(n>0)
{
return true;
}
else
{
return false;
}
}
博客介绍了自定义验证控件CustomValidator1的使用,包括选择文本框、出错显示等设置。还给出了服务器端验证代码,通过查询数据库判断用户名是否存在,若存在则验证不通过,不存在则通过。
417

被折叠的 条评论
为什么被折叠?



