小弟最近在做页面的验证,但是不太懂js,于是就想用C#的try catch方法来捕获。不知可否?大概的思路就是在cs文件里这样写:try{string  a = this.Name.text;//文本框dateTime b= this.date.value;//时间控件int c =Int32.Prase( this.Num.text);//文本框//数据操作省去}catch (Exception  )
            {                throw new Exception(“控件没有填值”);
            }问题是我怎么去获取是哪个控件填值有问题呢?请高手指教。

解决方案 »

  1.   

    用Validator, RegularExpressionValidator, RangeValidator,根据你的需要来
      

  2.   

    ErrorProvider 控件,自己可以控制控件的输入啊。
      

  3.   

    比较笨的方法
    foreach(Control c in Page.Controls)
    {
       TextBox tb=null;
      if(c.GetType()==typeof(TextBox))
       {
          tb=c as TextBox;
          if(tb.Text.Trim().length==0)
        {
          Response.Write(tb.ID+"没有赋值");
        }
       }
    }
    不推荐使用.net验证控件,增加很多额外代码而且不好操作,推荐使用Jquery的验证插件,楼主可以搜一下
      

  4.   

    通过RegularExpressionValidator结合正则判断还可使用
    string  a = this.Name.text;
    if(DateTIme.TryParse(this.date.value,out dt);)
    {
    }
    if(Int32.TryPrase( this.Num.text,out c))
    {
    }
    或遍历页面控件取值
    foreach(Control ctl in this.Page.Controls)
    {
    Type t=ctl.GetType();
    }