延续前面「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 WebControlsNamespace WebControls
11
Public Class TBCommandFieldClass 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()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()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()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()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()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

2

3

4

5

6

7

8

9

10


11


12

13

14

15

16


17

18

19

20

21

22

23


24

25

26

27

28

29

30

31

32


33

34

35

36

37

38

39


40

41

42

43

44

45

46

47

48

49

50

51


52

53

54

55

56

57

58

59

60

61


62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83


84

85

86


87

88

89

90


91

92

93

94


95

96

97

98

99

100

101

102

103

使用 TBCommandField 类别
因为 GridView.Columns 的属性编辑器不支持我们改写 TBCommandField,故只能切换至 aspx 程序代码中手动加入。

切换至设计画面,就可以看到 TBCommandField 的 Header 出现「新增」钮。

备注:若要让 GridView 的字段编辑器支持 TBCommandField,需自订 GridView.Columns 的属性编辑器。