All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Ajax的缺点_[AJAX教程]
document.getElementById的一些细节_JavaScript技术_编程技术
SQL Server2000连接中三个最常见错误分析_[SQL Server教程]
实例详解网页表单格式的应用技巧_[Html教程]
asp.net上传图片并同时生成缩略图_[Asp.Net教程]
ASP.NET应用技巧:JS完成ListBox内容的交互实例_.net资料_编程技术
asp.net2.0服务器控件之ListBox控件
定义列表 Definition Lists_[Html教程]
Asp.net生成静态网页的实现代码_.net资料_编程技术
《Effective,C#》之用委托实现回调_.net资料_编程技术
Reading number is top 10 pictures
战场废物2
Catch prostitution woman in China
再来随便发几张
Li Zongrui hunting video screenshots2
The money of more than 100 countries and regions14
漂亮脸蛋魔鬼身材1
Chinese paper-cut grilles art appreciation2
On the verge of extinction of the beach1
各种囧况!玩游戏最不喜欢出现的十件事(点评)
BingBingFan apple dew point photo gallery1
Download software ranking
Unix video tutorial14
WebService在.NET中的实战应用教学视频 → 第5集
Visual C++界面编程技术
Unix video tutorial17
White deer villiage
WebService在.NET中的实战应用教学视频 → 第1集
艳兽都市
Adobe Flash Player(IE) 10.0.32.18 浏览器专用的FLASH插件
Boxer's Top ten classic battle10
Ashlynn Video4
归海一刀 published in(发表于) 2014/1/30 1:02:04 Edit(编辑)
GridView+FormView,示范数据:,新增、修改、删除

GridView+FormView,示范数据:,新增、修改、删除

GridView+FormView 示范数据 新增/修改/删除_[Asp.Net教程]

摘要
在同一页面上以 GridView 配合 FormView 来完成数据的「新增/修改/删除」,在这个范例中有下列二个特点。
1. GridView 及 FormView 系结同一个 SqlDataSource 控件。
2. FormView 只使用 EditItemTemplate,同时来做新增及修改的动作。
范例程序代码: GridView1.rar

画面配置
此范例使用 Northwind 数据库的 Employees 数据表当作数据来源,在页面上放置 SqlDataSource、GridView、FormView,而 GridView 及 FormView 系结至同一个 SqlDataSource 控件。
GridView 的部分,启用分页模式,每页设为 5 笔,并将 CommandField 转为 TemplateField,然后在 HeaderTemplate 部分加入一个「新增」钮。
FormView 的部分在浏览模式为隐藏,设定属性 Visible="False"。执行新增及编辑时只会使用到 EditItemTemplate,故只保留 EditItemTemplate 的内容,然后设定属性 DefaultMode="Edit"。


aspx 程序代码如下


Code
1<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
2
3
4
5
6
7 未命名页面 <A href="">www.dwww.cn</A>
8
9
10
11

12 GridView+FormView 示范数据 新增/修改/删除

13

14

15 16 CellPadding="4" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" EmptyDataText="没有数据录可显示。"
17 ForeColor="#333333" GridLines="None" PageSize="5">
18
19
20
21
22
23 24 Text="新增">
25

26
27 28 Text="编辑">
29 30 Text="删除">
31

32

33 34 ReadOnly="True" SortExpression="EmployeeID" />
35
36
37
38

39
40
41
42
43
44

45 46 DataSourceID="SqlDataSource1" DefaultMode="Edit" ForeColor="#333333" Visible="False">
47
48
49
50

51

52

53 EmployeeID

54

55
56

57

58

59

60 LastName

61

62

63

64

65

66 FirstName

67

68

69

70

71

72 Title

73

74

75

76

77

78 79 Text="新增">
80 81 Text="更新">
82 83 Text="取消">
84

85
86
87

88 89 DeleteCommand="DELETE FROM [Employees] WHERE [EmployeeID] = @original_EmployeeID" InsertCommand="INSERT INTO [Employees] ([LastName], [FirstName], [Title]) VALUES (@LastName, @FirstName, @Title)"
90 ProviderName="<% ConnectionStrings:NorthwindConnectionString1.ProviderName %>"
91 SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title] FROM [Employees]"
92 UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [Title] = @Title WHERE [EmployeeID] = @original_EmployeeID" OldValuesParameterFormatString="original_{0}">
93
94
95

96
97
98
99
100

101
102
103
104
105
106

107

108

109
110

111
112
113


程序代码说明
GridView 的 CommandField 因为切换为 TemplateField,所以在 RowDataBound 事件中,需要去设定「编辑」钮的 CommandArgument 属性值为 RowIndex,GridView 的编辑动作才能正常执行。



1 Protected Sub GridView1_RowDataBound()Sub GridView1_RowDataBound(ByVal sender As Object, ByVal 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 '设定编辑钮的 CommandArgument
6 oButton = CType(e.Row.Cells(0).FindControl("btnEdit"), Button)
7 oButton.CommandArgument = e.Row.RowIndex.ToString
8 End If
9 End Sub
程序执行时预设为浏览模式,故只有显示 GridView,而 FormView 预设是隐藏。当 GridView 按下新增及编辑时,需要将 GirdView 隐藏,将 FormView 显示。所以在 GridView 的 RowCommand 事件中撰写如下程序代码。
执行「编辑」时,以 GetEditIndex 函式是取得 FormView 对应的编辑列索引,设定给 FormView.PageIndex,并将 FormView 切换为编辑模式。
执行「新增」钮,将 FormView.InsertItemTemplate 设为 FormView.EddiItemTemplate,即新增及编辑都使用同一个 Template,并将 FormView 切换为新增模式。


1 Protected Sub GridView1_RowCommand()Sub GridView1_RowCommand(ByVal sender As Object, ByVal 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 设为 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 /**/'''


22 ''' 取得编辑列索引。
23 '''

24 ''' GridView 控件。
25 ''' GridView 的数据列索引。
26 Private Function GetEditIndex()Function GetEditIndex(ByVal GridView As GridView, ByVal RowIndex As Integer) As Integer
27 Dim iEditIndex As Integer
28
29 If GridView.AllowPaging Then
30 'GridView 有分页时,要把考虑目前的页数及每页笔数
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 子控件的显示及隐藏状态。


1 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 /**/'''


2 ''' 切换为浏览模式。
3 '''

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 的编辑模式。



来源:http://www.cnblogs.com/jeff377







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.