最近在整理我写的几个服务器控件,发出来与大家共享吧。
我写的自定义服务器控件呢分为两个类,
一是“简单继承”控件,就是继承框架里的控件然后加点属性了、事件了什么的。
二是“功能”控件,简单的说那是符合控件,但是呢是以实现一种功能为目的地。这种控件要实现某一种功能,比如分页的功能,添加、修改数据的功能,查询的功能。
原先呢还不会使用接口了什么的,所以呢代码写得有点不太“好看”,现在使用接口、继承了什么的来整理一下。
先发一个“文本框”的吧,就是继承系统的 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 IGetControlValue
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
ReadOnly Property ControlKind() As String
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
Function GetControlValue() As String
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
Function GetControlValue(ByVal kind As String) As String
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
Sub SetControlValue(ByVal value As String)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
Sub SetControlValue(ByVal value As String, ByVal kind As String)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
End Interface
文本框的代码。
Imports
System.ComponentModel
Imports
System.Web.UI
Imports
HBS
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
<
DefaultProperty(
"
Text
"
), ToolboxData(
"
<{0}:HBSTextBox runat=server></{0}:HBSTextBox>
"
)
>
_
![](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
Public
Class HBSTextBox
Inherits System.Web.UI.WebControls.TextBox
Implements INamingContainer
Implements IGetControlValue
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
Dim _dataType As String = "101" '数据类型
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
实现接口
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
设置文本框的 Text 属性;获取 Text.Trim()
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
设置文本框的 Text 属性;获取 Text.Trim()
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
验证类型
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
验证错误的提示信息
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
Protected Overrides 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
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
End Sub
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
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是很好的选择。
以后会陆续说明其他的控件。
我写的自定义服务器控件呢分为两个类,
一是“简单继承”控件,就是继承框架里的控件然后加点属性了、事件了什么的。
二是“功能”控件,简单的说那是符合控件,但是呢是以实现一种功能为目的地。这种控件要实现某一种功能,比如分页的功能,添加、修改数据的功能,查询的功能。
原先呢还不会使用接口了什么的,所以呢代码写得有点不太“好看”,现在使用接口、继承了什么的来整理一下。
先发一个“文本框”的吧,就是继承系统的 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)给文本框赋值
取值和赋值为什么会有两套呢?那是为了和其他控件兼容,说到后面就好理解了。
定义接口。
![](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_1c53668bcee393edac0d7b3b3daff1ae.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_05dd8d549cff04457a6366b0a7c9352a.gif?x-oss-process=image/resize,w_1400/format,webp)
文本框的代码。
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_1c53668bcee393edac0d7b3b3daff1ae.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_7a2b9a960ee9a98bfd25d306d55009f8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_105a1e124122b2abcee4ea8e9f5108f3.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_05dd8d549cff04457a6366b0a7c9352a.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
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是很好的选择。
以后会陆续说明其他的控件。