最近在整理我写的几个服务器控件,发出来与大家共享吧。
我写的自定义服务器控件呢分为两个类,
一是“简单继承”控件,就是继承框架里的控件然后加点属性了、事件了什么的。
二是“功能”控件,简单的说那是符合控件,但是呢是以实现一种功能为目的地。这种控件要实现某一种功能,比如分页的功能,添加、修改数据的功能,查询的功能。
原先呢还不会使用接口了什么的,所以呢代码写得有点不太“好看”,现在使用接口、继承了什么的来整理一下。
先发一个“文本框”的吧,就是继承系统的 TextBox,然后加上几个属性、方法。
属性:
TextTrim 返回
.Text.Trim()
TextTrimNone 返回
.Text.Trim.Replace("'", "")
CheckDataType 设置 文本框的验证类型。
CheckDataReg 设置 文本框的验证用的正则表达式。
CheckErrorMessage 设置 当没有通过验证时显示给客户的提示信息。
属性(接口):
ControlKind 返回控件的类型。
函数(接口):
GetValue() 获取文本框的值,默认返回 TextTrim
GetValue(ByVal kind As String) 根据 kind 获取对应的文本框的值。
SetValue(ByVal value As String) 给文本框赋值
SetValue(ByVal value As String, ByVal kind As String)给文本框赋值
取值和赋值为什么会有两套呢?那是为了和其他控件兼容,说到后面就好理解了。
定义接口。

Public Interface IGetControlValueInterface IGetControlValue


ReadOnly Property ControlKind()Property ControlKind() As String


Function GetControlValue()Function GetControlValue() As String

Function GetControlValue()Function GetControlValue(ByVal kind As String) As String


Sub SetControlValue()Sub SetControlValue(ByVal value As String)

Sub SetControlValue()Sub SetControlValue(ByVal value As String, ByVal kind As String)

End Interface
文本框的代码。
Imports System.ComponentModel
Imports System.Web.UI
Imports HBS

<DefaultProperty("Text"), ToolboxData("<{0}:HBSTextBox runat=server></{0}:HBSTextBox>")> _

Public Class HBSTextBoxClass HBSTextBox
Inherits System.Web.UI.WebControls.TextBox
Implements INamingContainer
Implements IGetControlValue

Dim _dataType As String = "101" '数据类型


实现接口#Region "实现接口"
<Bindable(True), Category("默认值"), DefaultValue("201"), Description("获取控件类别")> _

ReadOnly Property ControlKind()Property ControlKind() As String Implements IGetControlValue.ControlKind
Get
Return "201"
End Get
End Property


Public Function GetValue()Function GetValue() As String Implements IGetControlValue.GetControlValue
Return Me.TextTrimNone

End Function


Public Function GetValue()Function GetValue(ByVal kind As String) As String Implements IGetControlValue.GetControlValue
Return Me.TextTrimNone

End Function


Public Sub SetValue()Sub SetValue(ByVal value As String) Implements IGetControlValue.SetControlValue
Me.Text = value

End Sub


Public Sub SetValue()Sub SetValue(ByVal value As String, ByVal kind As String) Implements IGetControlValue.SetControlValue
Me.Text = value

End Sub

#End Region


设置文本框的 Text 属性;获取 Text.Trim()#Region "设置文本框的 Text 属性;获取 Text.Trim()"
<Bindable(True), Category("文本值"), Description("设置文本框的 Text 属性;返回 Text.Trim()。")> _

Property TextTrim()Property TextTrim() As String
Set(ByVal Value As String)
Me.Text = Value
End Set
Get
Return Me.Text.Trim()
End Get
End Property
#End Region


设置文本框的 Text 属性;获取 Text.Trim()#Region "设置文本框的 Text 属性;获取 Text.Trim()"
<Bindable(True), Category("文本值"), Description("设置文本框的 Text 属性;返回 Text.Trim()。")> _

Property TextTrimNone()Property TextTrimNone() As String
Set(ByVal Value As String)
Me.Text = Value
End Set
Get
Return Me.Text.Trim.Replace("'", "")
End Get
End Property
#End Region



验证类型#Region "验证类型"
<Category("数据验证"), Description("验证输入的内容,需要js脚本配合。"), DefaultValue("101")> _

Property CheckDataType()Property CheckDataType() As String
Set(ByVal Value As String)
_dataType = Value
'101 b不验证
'102 z自然数
'103 z整数
'104 x小数
'105 r日期
'106 b必填
Select Case Value
Case "101" '不验证
'this.Attributes.Add("DataType","");
Case "102" '自然数
Me.Attributes.Add("check", "^[0-9]+$")
Case "103" '整数
Me.Attributes.Add("check", "^\S?([0-9]+)$")
Case "104" '小数 [0-9]{1,5}\.[0-9]{1,2} ^-?([0]$|^([0-9]{1,5}\.[0-9]{1,2})?$
Me.Attributes.Add("check", "^\S(\-?[0-9]*(\.[0-9]*)?)$")
'Me.Attributes.Add("check", "^d")
Case "105" '时间
Me.Attributes.Add("check", "^d{4}-d{1,2}-d{1,2}")
Case "106" '必填项
Me.Attributes.Add("check", ".+")
End Select

End Set
Get
If _dataType Is Nothing Then
Return ""
End If
Return _dataType
End Get
End Property

<Category("数据验证"), Description("验证输入的内容,传入正则表达式。"), DefaultValue("0")> _

Property CheckDataReg()Property CheckDataReg() As String
Set(ByVal Value As String)
Me.Attributes.Add("check", Value)
End Set
Get
If Me.Attributes.Item("check") Is Nothing Then
Return ""
End If
Return Me.Attributes.Item("check").ToString()
End Get
End Property


#End Region


验证错误的提示信息#Region "验证错误的提示信息"
<Description("验证错误提示信息")> _

Property CheckErrorMessage()Property CheckErrorMessage() As String

'添加提示信息
Set(ByVal Value As String)
ViewState("msg") = Value
Me.Attributes.Add("warning", Value)
End Set
Get
If (ViewState("msg") Is Nothing) Then
Return ""
Else
Return ViewState("msg").ToString()
End If
End Get

End Property
#End Region


Protected Overrides Sub OnInit()Sub OnInit(ByVal e As EventArgs)
If Not Me.CssClass Is Nothing Then
Me.CssClass = "txts"
Me.Attributes.Add("DataID", "")
Me.Attributes.Add("DataIDs", "")
End If

End Sub


End Class

FAQ:
1、
文本框的验证是如何实现的?
使用正则表达式来验证,在具体点说呢就是使用了一个不知道是谁写一个js函数,就是 checkForm(oForm) 来验证的。
http://www.cnblogs.com/jyk/archive/2007/09/20/899406.html 这里有具体的说明。
2、为什么使用VB.net来写?
很简单,好写。用VB.net写代码还是很方便的,也是很宽松的。好多在C#里不能通过编译的代码,在VB.net里面就可以正常使用,而且提示也是很“智能”的,当初刚写控件的时候还是不太熟悉,VB.net是很好的选择。
以后会陆续说明其他的控件。