最进做验证textbox的时候老是要求验证,单个拖控件工作量太大了
谁能帮我提供1个比较简单的textbox控件,只要实现对该textbox验证不能为空或着只能输入数字

解决方案 »

  1.   

    自己把TextBox和RequireFieldValidor封装成一个控件就行了
      

  2.   

    比较简单啊.俺是先建立一个类关系图,然后生成框架,再修改,这样可以利用vs的自动代码生成功能.
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ComponentModel;namespace SutsComponent
    {
        public class SutsNumberBox : System.Windows.Forms.TextBox
        {
            public SutsNumberBox()
                : base()
            {
                InitializeComponent();
            }
            private void InitializeComponent()
            {
                this.SuspendLayout();
                // 
                // SutsNumberBox
                // 
                this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
                this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.SutsNumberBox_KeyPress);
                this.TextChanged += new System.EventHandler(this.SutsNumberBox_TextChanged);
                this.ResumeLayout(false);        }        private void SutsNumberBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
                if ((e.KeyChar >= '0' && e.KeyChar <= '9') ||
                    (e.KeyChar == '.' && Text.IndexOf('.') < 0) ||
                    e.KeyChar == '\b' || e.KeyChar == '\r' || e.KeyChar == '\t')
                {
                    e.Handled = false;
                }
                else
                    e.Handled = true;
            }        private void SutsNumberBox_TextChanged(object sender, EventArgs e)
            {
                if (Text == "")
                {
                    Text = "0";
                }
            }        public double GetValue()
            {
                try
                {
                    return Convert.ToDouble(Text.Trim());
                }
                catch
                {
                    return 0;
                }
            }        [Description("得到或设置文本框中的数值!"), Category("表达式")]
            public double DoubleValue
            {
                set
                {
                    Text = value.ToString();
                }
                get
                {
                    return GetValue();
                }
            }
        }
    }
      

  3.   

    用用户控件来做,把TEXTBOX结合JS或RequireFieldValidor来做
    或自己写自定义控件.类似4楼的.
      

  4.   

    复合控件,可以参考这个方法,讲得还算详细
    http://hi.baidu.com/5imilan/blog/item/7a2de6dd342a28305982dde6.html
    记得还有一篇讲得更好的,现在找不到了
      

  5.   

    可以参考discuz!NT的源码
    里面就有这个集成验证功能的TextBox控件
      

  6.   


     public class CustomerControl:Control 
        {
            /// <summary>
            /// 要应用控件的状态,必须重写三个方法 方法1 OnInit()
            /// 在页面注册 将控件注册为持久化地控件
            /// </summary>
            /// <param name="e"></param>
            protected override void OnInit(EventArgs e)
            {
                Page.RegisterRequiresControlState(this);
                base.OnInit(e);
            }
            protected override object  SaveControlState()
            {
                return this.Text;
            }
            protected override void  LoadControlState(object savedState)
            {
                this.Text = savedState.ToString();
            }     
            public virtual string Text
            {
                get
                {
                    if (ViewState["Text"] != null)
                    {
                        return ViewState["Text"].ToString();
                    }
                    return null;
                }
                set
                {
                    if ((value != "") && (是否数字))
                    {
                        ViewState["Text"] = value;
                    }
                    else
                    {
                        ViewState["Text"] = "";
                    }                
                }        }      
            protected override void Render(HtmlTextWriter writer)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
                writer.AddAttribute(HtmlTextWriterAttribute.Width, "150");
                writer.AddAttribute(HtmlTextWriterAttribute.Id, "text1");
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
                writer.RenderBeginTag (HtmlTextWriterTag.Input);
                writer.RenderEndTag ();
                writer.RenderEndTag ();
            }    }