实现带验证功能的自定义文本框

刚刚学习了一下.NET自定义控件的写法,所以顺手做了一个简单的带验证功能的文本输入框,该控件主要包括以下功能:

1、允许设置文本框标签的显示内容

2、允许设置是否显示必填提示,类似* 效果

3、允许设置是否使用不为空验证

多余的话我就不说了,附上整段代码,不是很复杂,希望能做到一个抛砖引玉的作用:)

首先,创建自己的类库,其中namespace为MyControls,这样方面今后的调用,然后在创建一个WEB自定义控件,文件名为TextValidate.cs,代码内容如下:

  1. using  System;  
  2. using  System.Collections.Generic;  
  3. using  System.ComponentModel;  
  4. using  System.Text;  
  5. using  System.Web;  
  6. using  System.Web.UI;  
  7. using  System.Web.UI.WebControls;  
  8.   
  9. namespace  MyControls  
  10. {  
  11.     [DefaultProperty("Text" )]  
  12.     [ToolboxData("<{0}:TextValidate runat=server></{0}:TextValidate>" )]  
  13.     public   class  TextValidate : WebControl, INamingContainer  
  14.     {  
  15.         #region 使用的控件   
  16.         Label m_lbl文本标题;  
  17.         TextBox m_tb文本内容;  
  18.         Label m_lbl必填;  
  19.         RequiredFieldValidator m_rfv验证;  
  20.         #endregion   
  21.  
  22.         #region 自定义标签属性   
  23.         [Description("标签名称" )]  
  24.         public   string  LabelName  
  25.         {  
  26.             get   
  27.             {  
  28.                 EnsureChildControls();  
  29.                 return  m_lbl文本标题.Text;  
  30.             }  
  31.             set   
  32.             {  
  33.                 EnsureChildControls();  
  34.                 m_lbl文本标题.Text = value;  
  35.             }  
  36.         }  
  37.   
  38.         [Description("文本框内容" )]  
  39.         public   string  TextBoxValue  
  40.         {  
  41.             get   
  42.             {  
  43.                 EnsureChildControls();  
  44.                 return  m_tb文本内容.Text;  
  45.             }  
  46.             set   
  47.             {  
  48.                 EnsureChildControls();  
  49.                 m_tb文本内容.Text = value;  
  50.             }  
  51.         }  
  52.   
  53.         [Description("是否显示必填提示" )]  
  54.         public   bool  IsRequired  
  55.         {  
  56.             get   
  57.             {  
  58.                 EnsureChildControls();  
  59.                 return  m_lbl必填.Visible;  
  60.             }  
  61.             set   
  62.             {  
  63.                 EnsureChildControls();  
  64.                 m_lbl必填.Visible = value;  
  65.             }  
  66.         }  
  67.   
  68.         [Description("文本框类型" )]  
  69.         public  TextBoxMode TextBoxType  
  70.         {  
  71.             get   
  72.             {  
  73.                 EnsureChildControls();  
  74.                 return  m_tb文本内容.TextMode;  
  75.             }  
  76.             set   
  77.             {  
  78.                 EnsureChildControls();  
  79.                 m_tb文本内容.TextMode = value;  
  80.             }  
  81.         }  
  82.   
  83.         [Description("错误提示信息" )]  
  84.         public   string  ErrorInfo  
  85.         {  
  86.             get   
  87.             {  
  88.                 EnsureChildControls();  
  89.                 return  m_rfv验证.ErrorMessage;  
  90.             }  
  91.             set   
  92.             {  
  93.                 EnsureChildControls();  
  94.                 m_rfv验证.ErrorMessage = value;  
  95.             }  
  96.         }  
  97.   
  98.         [Description("是否启动不为空验证" )]  
  99.         public   bool  IsRequiredValidate  
  100.         {  
  101.             get   
  102.             {  
  103.                 EnsureChildControls();  
  104.                 return  m_rfv验证.Visible;  
  105.             }  
  106.             set   
  107.             {  
  108.                 EnsureChildControls();  
  109.                 m_rfv验证.Visible = value;  
  110.             }  
  111.         }  
  112.         #endregion   
  113.  
  114.         #region 重写CreateChildControls方法,用于初始化控件   
  115.         protected   override   void  CreateChildControls()  
  116.         {  
  117.             Controls.Clear();  
  118.   
  119.             m_lbl文本标题 = new  Label();  
  120.             m_lbl文本标题.Text = "" ;  
  121.             m_lbl文本标题.ID = "m_lbl文本标题" ;  
  122.             Controls.Add(m_lbl文本标题);  
  123.   
  124.             m_tb文本内容 = new  TextBox();  
  125.             m_tb文本内容.Text = "" ;  
  126.             m_tb文本内容.ID = "m_tb文本内容" ;  
  127.             m_tb文本内容.TextMode = TextBoxMode.SingleLine;  
  128.             Controls.Add(m_tb文本内容);  
  129.   
  130.             m_lbl必填 = new  Label();  
  131.             m_lbl必填.Text = "*" ;  
  132.             m_lbl必填.ForeColor = System.Drawing.Color.Red;  
  133.             m_lbl必填.Visible = false ;  
  134.             m_lbl必填.ID = "m_lbl必填" ;  
  135.             Controls.Add(m_lbl必填);  
  136.   
  137.             m_rfv验证 = new  RequiredFieldValidator();  
  138.             m_rfv验证.ControlToValidate = "m_tb文本内容" ;  
  139.             m_rfv验证.ErrorMessage = "" ;  
  140.             m_rfv验证.Visible = false ;  
  141.             m_rfv验证.SetFocusOnError = true ;  
  142.             m_rfv验证.ID = "m_rfv验证" ;  
  143.             Controls.Add(m_rfv验证);  
  144.   
  145.             ChildControlsCreated = true ;  
  146.         }  
  147.         #endregion   
  148.  
  149.         #region 组合控件所在容器,这里是将这一组控件放到DIV中   
  150.         protected   override  HtmlTextWriterTag TagKey  
  151.         {  
  152.             get   
  153.             {  
  154.                 return  HtmlTextWriterTag.Div;  
  155.             }  
  156.         }  
  157.         #endregion   
  158.  
  159.         #region 重写RenderContents方法,用于输出控件   
  160.         protected   override   void  RenderContents(HtmlTextWriter output)  
  161.         {  
  162.             m_lbl文本标题.RenderControl(output);  
  163.             m_tb文本内容.RenderControl(output);  
  164.             m_lbl必填.RenderControl(output);  
  165.             m_rfv验证.RenderControl(output);  
  166.         }  
  167.         #endregion   
  168.     }  
  169. }  

然后进行编译,然后在WEB项目中,点击“添加引用”,将我们做好的DLL引进来,这样就可以使用我们的自己的控件库了,然后调用的代码如下:

  1. <%@ Page Language= "C#"  AutoEventWireup= "true"  CodeFile= "Default.aspx.cs"  Inherits= "_Default"  %>  
  2. <%@ Register Assembly="MyControls"  Namespace= "MyControls"  TagPrefix= "my"  %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml"  >  
  6. <head runat="server" >  
  7.     <title>自定义控件</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1"  runat= "server" >  
  11.     <div>  
  12.     <my:TextValidate ID="m_tv用户名"  runat= "server"  LabelName= "用户名"  />  
  13.     <my:TextValidate ID="m_tv密码"  runat= "server"  LabelName= "密码"  TextBoxType= "Password"  IsRequired= "true"  IsRequiredValidate= "true"  ErrorInfo= "密码不能为空!"  />  
  14.     <asp:Button ID="submit"  runat= "server"  Text= "验证提交"  />  
  15.     </div>  
  16.     </form>  
  17. </body>  
  18. </html>  

这样就可以看到效果了,大家不妨试一下,显示效果如下图:

easyphper,EasyPHP,cgera,EL-TEAM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值