Protected Sub FormView1_PreRender()Sub FormView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
2 Dim oFormView As FormView
3 Dim oLinkButton As LinkButton
4 Dim oTextBox As TextBox
5
6 oFormView = CType(sender, FormView)
7 If Not oFormView.Visible Then Exit Sub
8
9 Select Case oFormView.CurrentMode
10 Case FormViewMode.Edit '编辑模式
11 '隐藏新增钮
12 oLinkButton = oFormView.FindControl("InsertButton")
13 oLinkButton.Visible = False
14 '显示更新钮
15 oLinkButton = oFormView.FindControl("UpdateButton")
16 oLinkButton.Visible = True
17 '显示 EmployeeID 的 TextBox
18 oTextBox = oFormView.FindControl("txtEmployeeID")
19 oTextBox.Visible = False
20 Case FormViewMode.Insert
21 '显示新增钮
22 oLinkButton = oFormView.FindControl("InsertButton")
23 oLinkButton.Visible = True
24 '隐藏更新钮
25 oLinkButton = oFormView.FindControl("UpdateButton")
26 oLinkButton.Visible = False
27 '显示 EmployeeID 的 TextBox
28 oTextBox = oFormView.FindControl("txtEmployeeID")
29 oTextBox.Visible = True
30 End Select
31 End Sub
当 FormView 执行「新增」、「更新」、「取消」钮的动作后,需要切换回浏览模式,即将 FormView 隐藏,而显示 GridView,相关程序代码如下。
1 /**/''' <summary>
2 ''' 切换为浏览模式。
3 ''' </summary>
4 Private Sub ChangeViewMode()Sub ChangeViewMode()
5 FormView1.Visible = False
6 GridView1.Visible = True
7 GridView1.EditIndex = -1
8 End Sub
9
10 Protected Sub FormView1_ItemCommand()Sub FormView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
11 If e.CommandName.ToUpper = "Cancel".ToUpper Then
12 '取消后,切换为浏览模式
13 ChangeViewMode()
14 End If
15 End Sub
16
17 Protected Sub FormView1_ItemInserted()Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles FormView1.ItemInserted
18 '新增后,切换为浏览模式
19 ChangeViewMode()
20 End Sub
21
22 Protected Sub FormView1_ItemUpdated()Sub FormView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles FormView1.ItemUpdated
23 '更新后,切换为浏览模式
24 ChangeViewMode()
25 End Sub
执行程序
执行程序预设为浏览模式![]()
按下 GridView 的「新增」钮时,即会切换到 FormView 的新增模式。![]()
按钮 GridView 的「编辑」钮时,即会切换到 FormView 的编辑模式。![]()
RSS订阅






收 藏
推 荐