摘要
延续前面「GridView+FormView 示范数据 新增/修改/删除(进阶篇:服务器控件)」的文章,文章后记有提及若要达到零程序代码要求,其中一个要件就是需要扩展 CommandField 类别,在 CommandField 的 Header 的部分加入「新增」钮,本文就是在说明如何扩展 CommandField 类别达到此需求。
扩展 CommandField 类别
我们的需求是在 CommandField 的 Header 加入新增钮,作法是继承 CommandField 下来命名为 TBCommandField,新增 ShowHeaderInsertButton 属性,来设定 Header 是否显示新增钮。TBCommandField 的程序代码如下,作法是覆写 InitializeCell 方法,若为 Header 且有设定 ShowHeaderInsertButton="True" 则利用 AddButtonToCell 私有方法来加入「新增」钮。
使用 TBCommandField 类别
因为 GridView.Columns 的属性编辑器不支持我们改写 TBCommandField,故只能切换至 aspx 程序代码中手动加入。
<bee:TBCommandField ShowHeaderInsertButton="True" InsertText="新增" ShowEditButton="True" ShowDeleteButton="True" ButtonType="Button" />
切换至设计画面,就可以看到 TBCommandField 的 Header 出现「新增」钮。

备注:若要让 GridView 的字段编辑器支持 TBCommandField,需自订 GridView.Columns 的属性编辑器。
延续前面「GridView+FormView 示范数据 新增/修改/删除(进阶篇:服务器控件)」的文章,文章后记有提及若要达到零程序代码要求,其中一个要件就是需要扩展 CommandField 类别,在 CommandField 的 Header 的部分加入「新增」钮,本文就是在说明如何扩展 CommandField 类别达到此需求。
扩展 CommandField 类别
我们的需求是在 CommandField 的 Header 加入新增钮,作法是继承 CommandField 下来命名为 TBCommandField,新增 ShowHeaderInsertButton 属性,来设定 Header 是否显示新增钮。TBCommandField 的程序代码如下,作法是覆写 InitializeCell 方法,若为 Header 且有设定 ShowHeaderInsertButton="True" 则利用 AddButtonToCell 私有方法来加入「新增」钮。
1
Imports System
2
Imports System.Collections.Generic
3
Imports System.ComponentModel
4
Imports System.Text
5
Imports System.Web
6
Imports System.Web.UI
7
Imports System.Web.UI.WebControls
8
Imports System.Globalization
9
10
Namespace WebControls
11
Public Class TBCommandField
12
Inherits CommandField
13
14
Private FShowHeaderInsertButton As Boolean = False
15
16
''' <summary>
17
''' 初始化储存格。
18
''' </summary>
19
''' <param name="cell">要初始化的储存格。</param>
20
''' <param name="cellType">储存格类型。</param>
21
''' <param name="rowState">储存格状态。</param>
22
''' <param name="rowIndex">数据列之以零起始的索引。</param>
23
Public Overrides Sub InitializeCell(ByVal cell As DataControlFieldCell, ByVal cellType As DataControlCellType, ByVal rowState As DataControlRowState, ByVal rowIndex As Integer)
24
MyBase.InitializeCell(cell, cellType, rowState, rowIndex)
25
26
If Me.ShowHeaderInsertButton AndAlso (cellType = DataControlCellType.Header) Then
27
'标题加入新增钮
28
AddButtonToCell(cell, "New", Me.InsertText, Me.CausesValidation, Me.ValidationGroup, rowIndex, Me.InsertImageUrl)
29
End If
30
End Sub
31
32
''' <summary>
33
''' 标题储存格是否显示新增钮。
34
''' </summary>
35
< _
36
Description("标题储存格是否显示新增钮"), _
37
DefaultValue(False) _
38
> _
39
Public Property ShowHeaderInsertButton() As Boolean
40
Get
41
Return FShowHeaderInsertButton
42
End Get
43
Set(ByVal value As Boolean)
44
If FShowHeaderInsertButton <> value Then
45
FShowHeaderInsertButton = value
46
Me.OnFieldChanged()
47
End If
48
End Set
49
End Property
50
51
''' <summary>
52
''' 储存格加入按钮。
53
''' </summary>
54
''' <param name="Cell">储存格。</param>
55
''' <param name="CommandName">按钮命令。</param>
56
''' <param name="ButtonText">按钮文字。</param>
57
''' <param name="CausesValidation">是否执行验证。</param>
58
''' <param name="ValidationGroup">验证控件所属之验证群组的名称。</param>
59
''' <param name="RowIndex">列索引。</param>
60
''' <param name="ImageUrl">影像网址。</param>
61
Private Sub AddButtonToCell(ByVal Cell As DataControlFieldCell, ByVal CommandName As String, ByVal ButtonText As String, ByVal CausesValidation As Boolean, ByVal ValidationGroup As String, ByVal RowIndex As Integer, ByVal ImageUrl As String)
62
Dim oButtonControl As IButtonControl
63
64
Select Case Me.ButtonType
65
Case ButtonType.Button
66
oButtonControl = New Button()
67
Exit Select
68
Case ButtonType.Link
69
oButtonControl = New LinkButton()
70
Case Else
71
oButtonControl = New ImageButton()
72
DirectCast(oButtonControl, ImageButton).ImageUrl = ImageUrl
73
Exit Select
74
End Select
75
oButtonControl.Text = ButtonText
76
oButtonControl.CommandName = CommandName
77
oButtonControl.CommandArgument = RowIndex.ToString(CultureInfo.InvariantCulture)
78
oButtonControl.CausesValidation = CausesValidation
79
oButtonControl.ValidationGroup = ValidationGroup
80
Cell.Controls.Add(DirectCast(oButtonControl, WebControl))
81
End Sub
82
83
''' <summary>
84
''' 建立新的 TBCommandField 对象。
85
''' </summary>
86
Protected Overrides Function CreateField() As DataControlField
87
Return New TBCommandField()
88
End Function
89
90
''' <summary>
91
''' 将目前 TBCommandField 对象的属性复制到指定之 DataControlField 对象。
92
''' </summary>
93
''' <param name="newField">目的 DataControlField 对象。</param>
94
Protected Overrides Sub CopyProperties(ByVal NewField As DataControlField)
95
Dim oNewField As TBCommandField
96
97
oNewField = DirectCast(NewField, TBCommandField)
98
oNewField.ShowHeaderInsertButton = Me.ShowHeaderInsertButton
99
MyBase.CopyProperties(NewField)
100
End Sub
101
102
End Class
103
End Namespace
Imports System2
Imports System.Collections.Generic3
Imports System.ComponentModel4
Imports System.Text5
Imports System.Web6
Imports System.Web.UI7
Imports System.Web.UI.WebControls8
Imports System.Globalization9

10
Namespace WebControls11
Public Class TBCommandField12
Inherits CommandField13

14
Private FShowHeaderInsertButton As Boolean = False15

16
''' <summary>17
''' 初始化储存格。18
''' </summary>19
''' <param name="cell">要初始化的储存格。</param>20
''' <param name="cellType">储存格类型。</param>21
''' <param name="rowState">储存格状态。</param>22
''' <param name="rowIndex">数据列之以零起始的索引。</param>23
Public Overrides Sub InitializeCell(ByVal cell As DataControlFieldCell, ByVal cellType As DataControlCellType, ByVal rowState As DataControlRowState, ByVal rowIndex As Integer)24
MyBase.InitializeCell(cell, cellType, rowState, rowIndex)25

26
If Me.ShowHeaderInsertButton AndAlso (cellType = DataControlCellType.Header) Then27
'标题加入新增钮28
AddButtonToCell(cell, "New", Me.InsertText, Me.CausesValidation, Me.ValidationGroup, rowIndex, Me.InsertImageUrl)29
End If30
End Sub31

32
''' <summary>33
''' 标题储存格是否显示新增钮。34
''' </summary>35
< _36
Description("标题储存格是否显示新增钮"), _37
DefaultValue(False) _38
> _39
Public Property ShowHeaderInsertButton() As Boolean40
Get41
Return FShowHeaderInsertButton42
End Get43
Set(ByVal value As Boolean)44
If FShowHeaderInsertButton <> value Then45
FShowHeaderInsertButton = value46
Me.OnFieldChanged()47
End If48
End Set49
End Property50

51
''' <summary>52
''' 储存格加入按钮。53
''' </summary>54
''' <param name="Cell">储存格。</param>55
''' <param name="CommandName">按钮命令。</param>56
''' <param name="ButtonText">按钮文字。</param>57
''' <param name="CausesValidation">是否执行验证。</param>58
''' <param name="ValidationGroup">验证控件所属之验证群组的名称。</param>59
''' <param name="RowIndex">列索引。</param>60
''' <param name="ImageUrl">影像网址。</param>61
Private Sub AddButtonToCell(ByVal Cell As DataControlFieldCell, ByVal CommandName As String, ByVal ButtonText As String, ByVal CausesValidation As Boolean, ByVal ValidationGroup As String, ByVal RowIndex As Integer, ByVal ImageUrl As String)62
Dim oButtonControl As IButtonControl63

64
Select Case Me.ButtonType65
Case ButtonType.Button66
oButtonControl = New Button()67
Exit Select68
Case ButtonType.Link69
oButtonControl = New LinkButton()70
Case Else71
oButtonControl = New ImageButton()72
DirectCast(oButtonControl, ImageButton).ImageUrl = ImageUrl73
Exit Select74
End Select75
oButtonControl.Text = ButtonText76
oButtonControl.CommandName = CommandName77
oButtonControl.CommandArgument = RowIndex.ToString(CultureInfo.InvariantCulture)78
oButtonControl.CausesValidation = CausesValidation79
oButtonControl.ValidationGroup = ValidationGroup80
Cell.Controls.Add(DirectCast(oButtonControl, WebControl))81
End Sub82

83
''' <summary>84
''' 建立新的 TBCommandField 对象。 85
''' </summary>86
Protected Overrides Function CreateField() As DataControlField87
Return New TBCommandField()88
End Function89

90
''' <summary>91
''' 将目前 TBCommandField 对象的属性复制到指定之 DataControlField 对象。92
''' </summary>93
''' <param name="newField">目的 DataControlField 对象。</param>94
Protected Overrides Sub CopyProperties(ByVal NewField As DataControlField)95
Dim oNewField As TBCommandField96

97
oNewField = DirectCast(NewField, TBCommandField)98
oNewField.ShowHeaderInsertButton = Me.ShowHeaderInsertButton99
MyBase.CopyProperties(NewField)100
End Sub101

102
End Class103
End Namespace使用 TBCommandField 类别
因为 GridView.Columns 的属性编辑器不支持我们改写 TBCommandField,故只能切换至 aspx 程序代码中手动加入。
<bee:TBCommandField ShowHeaderInsertButton="True" InsertText="新增" ShowEditButton="True" ShowDeleteButton="True" ButtonType="Button" />切换至设计画面,就可以看到 TBCommandField 的 Header 出现「新增」钮。
备注:若要让 GridView 的字段编辑器支持 TBCommandField,需自订 GridView.Columns 的属性编辑器。

浙公网安备 33010602011771号