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

解决方案 »

  1.   

    老师你能解释一下args.IsValid = val == 0;是什么意思吗,谢谢啊!
      

  2.   

    第二行的关闭连接应该是MyConnection.Close();
    args.IsValid = val == 0 这样在写你就明白了args.IsValid = (val == 0);
    就是如果没有记录就验证成功(不知道你自定义的验证控件是不是这个意思)
      

  3.   

    你在按钮事件里是怎么写的,我给你一个示例:
    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>");
    }
    }
      

  4.   

    我试过了,如果你不加if(Page.IsValid)那么不管验证通没通过都会执行按钮事件里的代码
      

  5.   

    这就是我的按钮事件,有错吗?
    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>");

    }
      

  6.   

    所以你在private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args) {
    验证里写的是正确的,只要在按钮事件下加Page.IsValid就可以了
      

  7.   

    我在第七行中设置为args.IsValid = true后,不管是否重复都可以输入,一旦改为false后,当点按钮时就什么也进不了,我感觉下面的这个函数没有起作用一样:
    foreach  (DataRowView  datarow  in  dv)  
                   {  
                      txtName  =  datarow["车驾号"].ToString();  
                      if  (txtName  ==  args.Value)  
                       {  
                            args.IsValid  =false;  
                            Message.Text  =  "数据表中存在此驾号的车";  
                            break;  
                      }  
                   }  
      

  8.   

    我采用这种方式访问数据库里的记录有问题吗?
    foreach  (DataRowView  datarow  in  dv)  
                   {  
                      txtName  =  datarow["车驾号"].ToString();  
                      if  (txtName  ==  args.Value)  
                       {  
                            args.IsValid  =false;  
                            Message.Text  =  "数据表中存在此驾号的车";  
                            break;  
                      }  
                   }
      

  9.   

    出现这个问题的原因可能是根本就没进到foreach循环里,第七行改为true是正确的,你也可以这么写来判断:
    SqlCommand  MyCommand  =  new  SqlCommand("SELECT 车驾号 FROM  车辆信息表 where 车驾号=@Num",  myConnection);  
    MyCommand.Parameters.Add(new SqlParameter("@Num",args.Value));
    if(MyCommand.ExecuteScalar() != null)
        args.IsValid  =false;
      

  10.   

    不好意思,你能解释一下吗?MyCommand.Parameters.Add(new SqlParameter("@Num",args.Value));谢谢啊!
      

  11.   

    你可以在msdn上查查看SqlParameter,就会明白他的意思了,他是一个参数,SELECT 车驾号 FROM  车辆信息表 where 车驾号=@Num,MyCommand.ExecuteScalar()是返回第一行第一列,如果一条记录也没有的话就说明这个车驾号不存在,所以通过这个也能判断出来