1Imports System
 2Imports System.Collections.Generic
 3Imports System.ComponentModel
 4Imports System.Text
 5Imports System.Web
 6Imports System.Web.UI
 7Imports System.Web.UI.WebControls
 8Imports System.Drawing
 9Imports Bee.Lib.TBLibFunc
 10
 11Namespace WebControlsNamespace WebControls
 12 /**/''' 
 13 ''' 利用使用者定义的样板,显示数据来源中单一数据录的值。支持编辑、删除及插入数据录。 
 14 ''' 
 15 < _
 16 Description("TBFormView 控件"), _
 17 ToolboxData("<{0}:TBFormView runat=server>{0}:TBFormView>") _
 18 > _
 19 Public Class TBFormViewClass TBFormView
 20 Inherits FormView
 21 Private FGridView As GridView = Nothing
 22
 23 /**/''' 
 24 ''' 连结的 GridView 控件。
 25 ''' 
 26 Protected Friend Property GridView()Property GridView() As GridView
 27 Get
 28 Return FGridView
 29 End Get
 30 Set(ByVal value As GridView)
 31 FGridView = value
 32 End Set
 33 End Property
 34
 35 /**/''' 
 36 ''' 递归寻找指定 ID 的控件。
 37 ''' 
 38 ''' 父代控件。
 39 ''' 按钮命令名称。
 40 ''' 回传 ID 符合的控件,若未找到则传回 Nothing。
 41 Private Overloads Function FindButtonControl()Function FindButtonControl(ByVal Parent As System.Web.UI.Control, ByVal CommandName As String) As IButtonControl
 42 Dim oControl As System.Web.UI.Control = Nothing
 43 Dim oButtonControl As IButtonControl = Nothing
 44
 45 For Each oControl In Parent.Controls
 46 If (TypeOf oControl Is IButtonControl) Then
 47 oButtonControl = DirectCast(oControl, IButtonControl)
 48 If SameText(CommandName, oButtonControl.CommandName) Then
 49 Return oButtonControl
 50 End If
 51 Else
 52 If oControl.Controls.Count > 0 Then
 53 oButtonControl = FindButtonControl(oControl, CommandName)
 54 If oButtonControl IsNot Nothing Then
 55 Return oButtonControl
 56 End If
 57 End If
 58 End If
 59 Next
 60
 61 Return Nothing
 62 End Function
 63
 64 /**/''' 
 65 ''' 依 CommandName 寻找对应的按钮。
 66 ''' 
 67 ''' 按钮命令名称。
 68 Private Overloads Function FindButtonControl()Function FindButtonControl(ByVal CommandName As String) As IButtonControl
 69 Return FindButtonControl(Me, CommandName)
 70 End Function
 71
 72 /**/''' 
 73 ''' 覆写。引发 Load 事件。
 74 ''' 
 75 Protected Overrides Sub OnLoad()Sub OnLoad(ByVal e As EventArgs)
 76 '若预设为编辑模式,则将 InsertItemTemplate 设为 EditItemTemplate
 77 If Me.DefaultMode = FormViewMode.Edit Then
 78 Me.InsertItemTemplate = Me.EditItemTemplate
 79 End If
 80 MyBase.OnLoad(e)
 81 End Sub
 82
 83 /**/''' 
 84 ''' 覆写。引发 PreRender 事件。
 85 ''' 
 86 Protected Overrides Sub OnPreRender()Sub OnPreRender(ByVal e As EventArgs)
 87 Dim oButtonControl As IButtonControl
 88
 89 MyBase.OnPreRender(e)
 90
 91 If Me.Visible AndAlso Me.GridView IsNot Nothing Then
 92 Select Case Me.CurrentMode
 93 Case FormViewMode.Edit '编辑模式
 94 '隐藏新增钮
 95 oButtonControl = FindButtonControl("Insert")
 96 If oButtonControl IsNot Nothing Then
 97 DirectCast(oButtonControl, Control).Visible = False
 98 End If
 99 '显示更新钮
100 oButtonControl = FindButtonControl("Update")
101 If oButtonControl IsNot Nothing Then
102 DirectCast(oButtonControl, Control).Visible = True
103 End If
104 Case FormViewMode.Insert
105 '显示新增钮
106 oButtonControl = FindButtonControl("Insert")
107 If oButtonControl IsNot Nothing Then
108 DirectCast(oButtonControl, Control).Visible = True
109 End If
110 '隐藏更新钮
111 oButtonControl = FindButtonControl("Update")
112 If oButtonControl IsNot Nothing Then
113 DirectCast(oButtonControl, Control).Visible = False
114 End If
115 End Select
116 End If
117 End Sub
118
119 /**/''' 
120 ''' 切换为浏览模式。
121 ''' 
122 Private Sub ChangeViewMode()Sub ChangeViewMode()
123 Me.Visible = False
124 Me.GridView.Visible = True
125 Me.GridView.EditIndex = -1
126 End Sub
127
128 /**/''' 
129 ''' 覆写。引发 ItemInserted 事件。
130 ''' 
131 Protected Overrides Sub OnItemInserted()Sub OnItemInserted(ByVal e As FormViewInsertedEventArgs)
132 MyBase.OnItemInserted(e)
133 '切换为浏览模式
134 ChangeViewMode()
135 End Sub
136
137 /**/''' 
138 ''' 覆写。引发 ItemUpdated 事件。
139 ''' 
140 Protected Overrides Sub OnItemUpdated()Sub OnItemUpdated(ByVal e As FormViewUpdatedEventArgs)
141 MyBase.OnItemUpdated(e)
142 '切换为浏览模式
143 ChangeViewMode()
144 End Sub
145
146 /**/''' 
147 ''' 覆写。引发 ItemCommand 事件。
148 ''' 
149 Protected Overrides Sub OnItemCommand()Sub OnItemCommand(ByVal e As FormViewCommandEventArgs)
150 MyBase.OnItemCommand(e)
151 End Sub
152 End Class
153
154End Namespace
使用 TBGridView 及 TBFormView 控件
以上篇的范例程序做修改,只要将 aspx 中 GridView 置换为 TBGridView,而 FormView 置换为 TBFormView,并设定 TBGridView 的 FormViewID 属性为 TBFormView 控件 ID 即可。
 CellPadding="4" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" EmptyDataText="沒有資料錄可顯示。"
 ForeColor="#333333" GridLines="None" PageSize="5" FormViewID="FormView1">
而 aspx.vb 的程序代码可以简化如下,原本 GridView 及 FormView 相关操作的控管都可以省略掉。
 1Partial Class _DefaultClass _Default
 2 Inherits System.Web.UI.Page
 3
 4 Protected Sub GridView1_RowDataBound()Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
 5 Dim oButton As Button
 6
 7 If e.Row.RowType = DataControlRowType.DataRow Then
 8 '设定编辑钮的 CommandArgument
 9 oButton = CType(e.Row.Cells(0).FindControl("btnEdit"), Button)
10 oButton.CommandArgument = e.Row.RowIndex.ToString
11 End If
12 End Sub
13
14 Protected Sub FormView1_PreRender()Sub FormView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
15 Dim oFormView As FormView
16 Dim oTextBox As TextBox
17
18 oFormView = CType(sender, FormView)
19 If Not oFormView.Visible Then Exit Sub
20
21 Select Case oFormView.CurrentMode
22 Case FormViewMode.Edit '编辑模式
23 '显示 EmployeeID 的 TextBox
24 oTextBox = oFormView.FindControl("txtEmployeeID")
25 oTextBox.Visible = False
26 Case FormViewMode.Insert
27 '显示 EmployeeID 的 TextBox
28 oTextBox = oFormView.FindControl("txtEmployeeID")
29 oTextBox.Visible = True
30 End Select
31 End Sub
32End Class
后记
也许有人会问,可不可以连上述的程序代码都省略了,答案也是肯定的,只要去扩充 CommandField 及 TextBox 控件就可以达到零程序代码。对于 CommandField 的部分,要让 CommandField 的 Header 有辨法放「新增」钮;而 TextBox 的部分,要让 TextBox 有辨法自行判断所在的 FormView 的 CurrentMode,自行决定本身是否要显示或隐藏。