using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//引入样式资源(要先设置为嵌入式)
[assembly: WebResource("WebControls.CSS.Validate.css", "text/css")]
[assembly: WebResource("WebControls.JS.Validate.js", "text/javascript")]
namespace WebControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyTextBoxControl runat=server></{0}:MyTextBoxControl>")]
public class MyTextBoxControl : TextBox
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("Number")]
[Localizable(true)]
private ValidateType validateType;
/// <summary>
/// 对外暴露验证类型
/// </summary>
public ValidateType ValidateType
{
get { return validateType; }
set { validateType = value; }
}
/// <summary>
/// 控件预呈现
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
//为页面引入JS样式
Page.ClientScript.RegisterClientScriptResource(this.GetType(), "WebControls.JS.Validate.js");
//为页面引入CSS样式 using System.Web.UI.HtmlControls;
HtmlLink html = new HtmlLink();
//判断id是否存在 存在就不执行后面的东西
if ("vid".Equals(html.ID)) return;
//设置id
html.ID = "vid";
//设置href
html.Href = Page.ClientScript.GetWebResourceUrl(this.GetType(), "WebControls.CSS.Validate.css");
//设置type
html.Attributes.Add("type", "text/css");
//设置rel
html.Attributes.Add("rel", "stylesheet");
//将样式添加到页面头部
Page.Header.Controls.Add(html);
}
/// <summary>
/// 控件呈现
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
switch (this.ValidateType)
{
case ValidateType.Number:
writer.Write("请输入数字");
break;
case ValidateType.AllChar:
writer.Write("请输入字母");
break;
case ValidateType.BigChar:
writer.Write("请输入大写字母");
break;
default:
writer.Write("请输入小写字母");
break;
}
}
/// <summary>
/// 添加特性到呈现
/// </summary>
/// <param name="writer"></param>
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
//调用验证方法
writer.AddAttribute(HtmlTextWriterAttribute.Onchange, "ValidateText('" + this.ValidateType + "', this)");
//引入类样式
writer.AddAttribute(HtmlTextWriterAttribute.Class, "text");
}
}
/// <summary>
/// 验证枚举
/// </summary>
public enum ValidateType{
/// <summary>
/// 验证数字
/// </summary>
Number,
/// <summary>
/// 验证字母
/// </summary>
AllChar,
/// <summary>
/// 大写字母
/// </summary>
BigChar,
/// <summary>
/// 小写字母
/// </summary>
SmallChar
}
}
.aspx页面
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestMyWebService.aspx.cs" Async="true" Inherits="TestMyWebService" %>
2
3 <%@ Register Assembly="ValidateControl" Namespace="WebControls" TagPrefix="cc1" %>
4
5
6 <!DOCTYPE html>
7
8 <html xmlns="http://www.w3.org/1999/xhtml">
9 <head runat="server">
10 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
11 <title></title>
12 </head>
13 <body>
14 <form id="form1" runat="server">
15 <div>
16
17 <cc1:MyTextBoxControl ID="MyTextBoxControl1" runat="server" ValidateType="AllChar"></cc1:MyTextBoxControl>
67 <cc1:MyTextBoxControl ID="MyTextBoxControl2" runat="server" ValidateType="BigChar"></cc1:MyTextBoxControl>
68 <cc1:MyTextBoxControl ID="MyTextBoxControl3" runat="server" ValidateType="Number"></cc1:MyTextBoxControl>
69 <cc1:MyTextBoxControl ID="MyTextBoxControl4" runat="server" ValidateType="SmallChar"></cc1:MyTextBoxControl>
64
65 </div>
66
70
71
72 </form>
73 </body>
74 </html>
显示出来的页面