刚刚学习了一下.NET自定义控件的写法,所以顺手做了一个简单的带验证功能的文本输入框,该控件主要包括以下功能:
1、允许设置文本框标签的显示内容
2、允许设置是否显示必填提示,类似* 效果
3、允许设置是否使用不为空验证
多余的话我就不说了,附上整段代码,不是很复杂,希望能做到一个抛砖引玉的作用:)
首先,创建自己的类库,其中namespace为MyControls,这样方面今后的调用,然后在创建一个WEB自定义控件,文件名为TextValidate.cs,代码内容如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Text;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace MyControls
- {
- [DefaultProperty("Text" )]
- [ToolboxData("<{0}:TextValidate runat=server></{0}:TextValidate>" )]
- public class TextValidate : WebControl, INamingContainer
- {
- #region 使用的控件
- Label m_lbl文本标题;
- TextBox m_tb文本内容;
- Label m_lbl必填;
- RequiredFieldValidator m_rfv验证;
- #endregion
- #region 自定义标签属性
- [Description("标签名称" )]
- public string LabelName
- {
- get
- {
- EnsureChildControls();
- return m_lbl文本标题.Text;
- }
- set
- {
- EnsureChildControls();
- m_lbl文本标题.Text = value;
- }
- }
- [Description("文本框内容" )]
- public string TextBoxValue
- {
- get
- {
- EnsureChildControls();
- return m_tb文本内容.Text;
- }
- set
- {
- EnsureChildControls();
- m_tb文本内容.Text = value;
- }
- }
- [Description("是否显示必填提示" )]
- public bool IsRequired
- {
- get
- {
- EnsureChildControls();
- return m_lbl必填.Visible;
- }
- set
- {
- EnsureChildControls();
- m_lbl必填.Visible = value;
- }
- }
- [Description("文本框类型" )]
- public TextBoxMode TextBoxType
- {
- get
- {
- EnsureChildControls();
- return m_tb文本内容.TextMode;
- }
- set
- {
- EnsureChildControls();
- m_tb文本内容.TextMode = value;
- }
- }
- [Description("错误提示信息" )]
- public string ErrorInfo
- {
- get
- {
- EnsureChildControls();
- return m_rfv验证.ErrorMessage;
- }
- set
- {
- EnsureChildControls();
- m_rfv验证.ErrorMessage = value;
- }
- }
- [Description("是否启动不为空验证" )]
- public bool IsRequiredValidate
- {
- get
- {
- EnsureChildControls();
- return m_rfv验证.Visible;
- }
- set
- {
- EnsureChildControls();
- m_rfv验证.Visible = value;
- }
- }
- #endregion
- #region 重写CreateChildControls方法,用于初始化控件
- protected override void CreateChildControls()
- {
- Controls.Clear();
- m_lbl文本标题 = new Label();
- m_lbl文本标题.Text = "" ;
- m_lbl文本标题.ID = "m_lbl文本标题" ;
- Controls.Add(m_lbl文本标题);
- m_tb文本内容 = new TextBox();
- m_tb文本内容.Text = "" ;
- m_tb文本内容.ID = "m_tb文本内容" ;
- m_tb文本内容.TextMode = TextBoxMode.SingleLine;
- Controls.Add(m_tb文本内容);
- m_lbl必填 = new Label();
- m_lbl必填.Text = "*" ;
- m_lbl必填.ForeColor = System.Drawing.Color.Red;
- m_lbl必填.Visible = false ;
- m_lbl必填.ID = "m_lbl必填" ;
- Controls.Add(m_lbl必填);
- m_rfv验证 = new RequiredFieldValidator();
- m_rfv验证.ControlToValidate = "m_tb文本内容" ;
- m_rfv验证.ErrorMessage = "" ;
- m_rfv验证.Visible = false ;
- m_rfv验证.SetFocusOnError = true ;
- m_rfv验证.ID = "m_rfv验证" ;
- Controls.Add(m_rfv验证);
- ChildControlsCreated = true ;
- }
- #endregion
- #region 组合控件所在容器,这里是将这一组控件放到DIV中
- protected override HtmlTextWriterTag TagKey
- {
- get
- {
- return HtmlTextWriterTag.Div;
- }
- }
- #endregion
- #region 重写RenderContents方法,用于输出控件
- protected override void RenderContents(HtmlTextWriter output)
- {
- m_lbl文本标题.RenderControl(output);
- m_tb文本内容.RenderControl(output);
- m_lbl必填.RenderControl(output);
- m_rfv验证.RenderControl(output);
- }
- #endregion
- }
- }
然后进行编译,然后在WEB项目中,点击“添加引用”,将我们做好的DLL引进来,这样就可以使用我们的自己的控件库了,然后调用的代码如下:
- <%@ Page Language= "C#" AutoEventWireup= "true" CodeFile= "Default.aspx.cs" Inherits= "_Default" %>
- <%@ Register Assembly="MyControls" Namespace= "MyControls" TagPrefix= "my" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server" >
- <title>自定义控件</title>
- </head>
- <body>
- <form id="form1" runat= "server" >
- <div>
- <my:TextValidate ID="m_tv用户名" runat= "server" LabelName= "用户名" />
- <my:TextValidate ID="m_tv密码" runat= "server" LabelName= "密码" TextBoxType= "Password" IsRequired= "true" IsRequiredValidate= "true" ErrorInfo= "密码不能为空!" />
- <asp:Button ID="submit" runat= "server" Text= "验证提交" />
- </div>
- </form>
- </body>
- </html>
这样就可以看到效果了,大家不妨试一下,显示效果如下图: