private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
int val;
SqlConnection MyConnection = new SqlConnection ("server=xw;uid=sa;pwd=sa;database=Car");
MyConnection.Open();
SqlCommand cmd = new SqlCommand("SELECT count(*) FROM 车辆信息表 where 车驾号='" + args.Value + "'", MyConnection);
val = int.Parse(cmd.ExecuteScalar().ToString());
conn.Close();
args.IsValid = val == 0;
}
{
int val;
SqlConnection MyConnection = new SqlConnection ("server=xw;uid=sa;pwd=sa;database=Car");
MyConnection.Open();
SqlCommand cmd = new SqlCommand("SELECT count(*) FROM 车辆信息表 where 车驾号='" + args.Value + "'", MyConnection);
val = int.Parse(cmd.ExecuteScalar().ToString());
conn.Close();
args.IsValid = val == 0;
}
args.IsValid = val == 0 这样在写你就明白了args.IsValid = (val == 0);
就是如果没有记录就验证成功(不知道你自定义的验证控件是不是这个意思)
private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args) {
args.IsValid = true;
if(TextBox1.Text == "a"){
args.IsValid = false;
}
}
private void Button3_Click(object sender, System.EventArgs e) {
if(Page.IsValid){
Response.Write("<script>alert('ok');</script>");
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
string strCommand="insert 车辆信息表 values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"',"+TextBox5.Text+",'"+TextBox6.Text+"','"+TextBox7.Text+"','"+TextBox8.Text+"','"+TextBox9.Text+"','"+TextBox10.Text+"',"+TextBox11.Text+",'"+TextBox12.Text+"','"+TextBox13.Text+"','"+DropDownList1.SelectedItem.Value+"')";
SqlCommand addScore=new SqlCommand(strCommand,myConnection);
myConnection.Open();
addScore.ExecuteNonQuery();
myConnection.Close();
BindGrid("车辆信息表");
Label1.Text="车辆信息插入成功!";
TextBox1.Text="";
TextBox2.Text="";
TextBox3.Text="";
TextBox4.Text="";
TextBox5.Text="";
TextBox6.Text="";
TextBox7.Text="";
TextBox8.Text="";
TextBox9.Text="";
TextBox10.Text="";
TextBox11.Text="";
TextBox12.Text="";
TextBox13.Text="";
}
else
{
Response.Write("<script lanuage=javascript>alert('你所输入的有误请重新输入');</script>");
}
验证里写的是正确的,只要在按钮事件下加Page.IsValid就可以了
foreach (DataRowView datarow in dv)
{
txtName = datarow["车驾号"].ToString();
if (txtName == args.Value)
{
args.IsValid =false;
Message.Text = "数据表中存在此驾号的车";
break;
}
}
foreach (DataRowView datarow in dv)
{
txtName = datarow["车驾号"].ToString();
if (txtName == args.Value)
{
args.IsValid =false;
Message.Text = "数据表中存在此驾号的车";
break;
}
}
SqlCommand MyCommand = new SqlCommand("SELECT 车驾号 FROM 车辆信息表 where 车驾号=@Num", myConnection);
MyCommand.Parameters.Add(new SqlParameter("@Num",args.Value));
if(MyCommand.ExecuteScalar() != null)
args.IsValid =false;