1:在页面中需要一个输入日期的TextBOX。
2:扩展TextBOX,使其具有属性:1)控件名称--例如【生日】,数据类型【例如:DateTime】,是否允许为空,最大长度,最小长度:
<cc1:TextBoxEx ID="txtClassDate" OnClick="ShowDateTime(this,'Date');" TextID="生日" TypeName="DateTime" IsNotNull="true" MaxValueLength="255" MinValueLength="8" runat="server"></cc1:TextBoxEx>
3:根据属性生成alt属性字符串:
<input name="_ctl0:DefaultContent:txtClassDate" type="text" id="_ctl0_DefaultContent_txtClassDate" OnClick="ShowDateTime(this,'Date');" alt="生日;ColumnTextBox;DateTime;1;0;0;" />
ColumnTextBox表示控件类型为TextBox
字符串:生日;ColumnTextBox;DateTime;1;255;8;的含义:
控件对应的项:生日【生日】
控件类型为:文本框【ColumnTextBox】
输入控件中的数据类型为:日期类型【DateTime】
控件值不允许为空:【1】
控件值最大长度:255【255】
控件值最小长度:8【8】
4:通过Javascript获取alt字符串:
control = document.all("_ctl0_DefaultContent_txtClassDate");
var alt = control.alt;
5:将alt字符串分解为3表述的含义:
/
// 根据alt取得对象
//
function GetObjectFromAlt(alt)
{
var arr = alt.split( ";" );
var txtObj = new Object();
// 名称
txtObj.Text = arr[0];
// 控件类型
txtObj.ControlType = arr[1];
// 数据类型
txtObj.TypeName = arr[2];
// 是否不允许为空
txtObj.IsNotNull = arr[3];
// 最小长度
txtObj.MinLength = arr[4];
// 最大长度
txtObj.MaxLength = arr[5];
return txtObj;
}
var txtObj = GetObjectFromAlt(alt);
6:针对取得的txtObj对象进行判断输入数据是否合法
7:将页面验证代码抽象:
1)通过javascript取得所有页面控件:
var length = document.all.length;
for (var i = 0; i < length; i++)
{
var item = document.all(i);
}
2)判断控件类型是不是文本框:item.tagName == "INPUT"【因为ControlType为ColumnTextBox,即文本框】
3)判断控件alt属性是否包含“;”,如果包含,则表示是我们扩展的自定义文本框。则进行输入数据验证。
8:自定义TextBox的实现:
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Design;
using System.ComponentModel;
using System.IO;
using System.Data.OleDb;
using System.Web.UI;
namespace DOTNET.Web
{
/// <summary>
/// 扩展TextBox。
/// </summary>
/// <author> 天志 </author>
/// <log date="2008-01-12"> 创建 </log>
[ToolboxBitmap( typeof (DOTNET.Web.TextBoxEx), "" ),
ToolboxData( " <{0}:TextBoxEx runat=server></{0}:TextBoxEx> " )]
public class TextBoxEx : TextBox
{
private bool isNotNull = false ;
[Bindable( true ),
Category( " 是否必填 " ),
DefaultValue( " false " ),
Description( " 是否必填 " )]
public bool IsNotNull
{
get
{
return this .isNotNull;
}
set
{
this .isNotNull = value;
}
}
private string textID;
[Bindable( true ),
Category( " 文本ID " ),
Description( " 文本ID " )]
public string TextID
{
set
{
textID = value;
}
get
{
return textID;
}
}
private int maxValueLength = 0 ;
[Bindable( true ),
Category( " 最大长度 " ),
DefaultValue( " 0 " ),
Description( " 最大长度 " )]
public int MaxValueLength
{
get
{
return maxValueLength;
}
set
{
maxValueLength = value;
}
}
private int minValueLength = 0 ;
[Bindable( true ),
Category( " 最小长度 " ),
DefaultValue( " 0 " ),
Description( " 最小长度 " )]
public int MinValueLength
{
get
{
return minValueLength;
}
set
{
minValueLength = value;
}
}
private string typeName = " string " ;
[Bindable( true ),
Category( " 数据类型 " ),
DefaultValue( " string " ),
Description( " 数据类型 " )]
public string TypeName
{
get
{
return typeName;
}
set
{
typeName = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
string text = ResourceMng.GetNameByID( this .TextID) + " ; " ;
text += " ColumnTextBox; " ;
text += TypeName + " ; " ;
if ( this .isNotNull)
{
text += " 1; " ;
}
else
{
text += " 0; " ;
}
text += this .MinValueLength + " ; " ;
text += this .MaxValueLength + " ; " ;
base .Attributes[ " alt " ] = text;
base .Render(writer);
}
}
}