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;
}
}