国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
GridView+FormView 示范數(shù)據(jù) 新增/修改/刪除
畫面配置
此范例使用 Northwind 數(shù)據(jù)庫的 Employees 數(shù)據(jù)表當作數(shù)據(jù)來源,在頁面上放置 SqlDataSource、GridView、FormView,而 GridView 及 FormView 系結(jié)至同一個 SqlDataSource 控件。
GridView 的部分,啟用分頁模式,每頁設(shè)為 5 筆,并將 CommandField 轉(zhuǎn)為 TemplateField,然后在 HeaderTemplate 部分加入一個「新增」鈕。
FormView 的部分在瀏覽模式為隱藏,設(shè)定屬性 Visible="False"。執(zhí)行新增及編輯時只會使用到 EditItemTemplate,故只保留 EditItemTemplate 的內(nèi)容,然后設(shè)定屬性 DefaultMode="Edit"。


aspx 程序代碼如下
Code

程序代碼說明
GridView 的 CommandField 因為切換為 TemplateField,所以在 RowDataBound 事件中,需要去設(shè)定「編輯」鈕的 CommandArgument 屬性值為 RowIndex,GridView 的編輯動作才能正常執(zhí)行。

1
    Protected Sub GridView1_RowDataBound(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
2
        
Dim oButton As Button
3

4
        
If e.Row.RowType = DataControlRowType.DataRow Then
5
            
'設(shè)定編輯鈕的 CommandArgument
6
            oButton = CType(e.Row.Cells(0).FindControl("btnEdit"), Button)
7
            oButton.CommandArgument 
= e.Row.RowIndex.ToString
8
        
End If
9
    
End Sub

程序執(zhí)行時預設(shè)為瀏覽模式,故只有顯示 GridView,而 FormView 預設(shè)是隱藏。當 GridView 按下新增及編輯時,需要將 GirdView 隱藏,將 FormView 顯示。所以在 GridView 的 RowCommand 事件中撰寫如下程序代碼。
執(zhí)行「編輯」時,以 GetEditIndex 函式是取得 FormView 對應的編輯列索引,設(shè)定給 FormView.PageIndex,并將 FormView 切換為編輯模式。
執(zhí)行「新增」鈕,將 FormView.InsertItemTemplate 設(shè)為 FormView.EddiItemTemplate,即新增及編輯都使用同一個 Template,并將 FormView 切換為新增模式。

 1
    Protected Sub GridView1_RowCommand(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
 2
        
Dim iEditIndex As Integer
 3

 4
        
Select Case e.CommandName.ToUpper
 5
            
Case "Edit".ToUpper '編輯模式
 6
                iEditIndex = GetEditIndex(CType(sender, GridView), CInt(e.CommandArgument))
 7
                FormView1.PageIndex 
= iEditIndex
 8
                FormView1.ChangeMode(FormViewMode.Edit) 
'FormView 切換為編輯模式
 9
                FormView1.Visible = True  'FormView 顯示
10
                GridView1.Visible = False 'GridView 隱藏
11

12
            
Case "Insert".ToUpper '新增模式
13
                '因為只有使用 EditItemTemplate,故將 InsertItemTemplate 設(shè)為 EditItemTemplate
14
                FormView1.InsertItemTemplate = FormView1.EditItemTemplate
15
                FormView1.ChangeMode(FormViewMode.Insert) 
'FormView 切換為新增模式
16
                FormView1.Visible = True  'FormView 顯示
17
                GridView1.Visible = False 'GridView 隱藏
18
        End Select
19
    
End Sub

20

21
    
''' <summary>
22
    
''' 取得編輯列索引。
23
    
''' </summary>
24
    
''' <param name="GridView">GridView 控件。</param>
25
    
''' <param name="RowIndex">GridView 的數(shù)據(jù)列索引。</param>

26
    Private Function GetEditIndex(ByVal GridView As GridView, ByVal RowIndex As IntegerAs Integer
27
        
Dim iEditIndex As Integer
28

29
        
If GridView.AllowPaging Then
30
            
'GridView 有分頁時,要把考慮目前的頁數(shù)及每頁筆數(shù)
31
            iEditIndex = (GridView.PageIndex) * GridView.PageSize + RowIndex
32
        
Else
33
            
'GridView 無分頁時,直接使用 e.NewSelectedIndex
34
            iEditIndex = RowIndex
35
        
End If
36
        
Return iEditIndex
37
    
End Function

在 FormView 中因為只使用 EddiItemTemplate 來處理「新增」及「編輯」模式,做需要置放「新增」、「更新」、「取消」三個按鈕。
在「新增」模式顯示「新增」鈕與「取消」鈕,以及顯示 EmployeeID 字段的 TextBox。
在「編輯」模式顯示「更新」鈕與「取消」鈕。EmployeeID 字段為只讀,故隱藏 EmployeeID 字段的 TextBox。
針對以上的處理動作,在 FormView 的 PreRender 事件中撰寫如下程序代碼來處理 FormView 子控件的顯示及隱藏狀態(tài)。

 1
    Protected Sub FormView1_PreRender(ByVal sender As ObjectByVal 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 執(zhí)行「新增」、「更新」、「取消」鈕的動作后,需要切換回瀏覽模式,即將 FormView 隱藏,而顯示 GridView,相關(guān)程序代碼如下。

 1
    ''' <summary>
 2
    
''' 切換為瀏覽模式。
 3
    
''' </summary>

 4
    Private Sub ChangeViewMode()
 5
        FormView1.Visible 
= False
 6
        GridView1.Visible 
= True
 7
        GridView1.EditIndex 
= -1
 8
    
End Sub

 9

10
    
Protected Sub FormView1_ItemCommand(ByVal sender As ObjectByVal 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(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles FormView1.ItemInserted
18
        
'新增后,切換為瀏覽模式
19
        ChangeViewMode()
20
    
End Sub

21

22
    
Protected Sub FormView1_ItemUpdated(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles FormView1.ItemUpdated
23
        
'更新后,切換為瀏覽模式
24
        ChangeViewMode()
25
    
End Sub
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
VBA 添加日歷控件的操作
VB.NET版的GridView經(jīng)典使用(編輯,刪除,分頁,鏈接列)
輸入時逐步提示信息
ASP.NET 2.0數(shù)據(jù)處理之高級分頁與排序
asp.net 2.0 正式版中無刷新頁面的開發(fā)(示例代碼的補充) _asp.net技巧
泛型的排序問題
更多類似文章 >>
生活服務(wù)
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服