最近有一个页面,需要在表格的单元格里列出附属数据。由于开发速度上的考虑,我采用了GridView嵌套GridView的方案。前台代码举例如下:
注意:为集中精力说明本文的问题,下面的代码做了删剪,并不能实际运行
――――――――――――― 前台代码(grdList嵌套grdAttachList) Start ―――――――――――――
![]()
前台代码
1
<asp:GridView ID="grdList" runat="server" DataSourceID="sdsList" AutoGenerateColumns="False" DataKeyNames="docID" >
2![]()
3
<Columns>
4![]()
5
<asp:TemplateField HeaderText="文書名" SortExpression="docName">
6![]()
7
<ItemTemplate>
8![]()
9
<asp:GridView ID="grdAttachList" runat="server"
10![]()
11
DataKeyNames="attachID" OnRowDataBound="grdAttach_RowDataBound" OnRowCommand="grdAttach_RowCommand">
12![]()
13
<Columns>
14![]()
15
<asp:TemplateField HeaderText="付属図名">
16![]()
17
<ItemTemplate>
18![]()
19
<asp:LinkButton ID="lbtnAttach" runat="server" CausesValidation="false" CommandName="attachDownload"/>
20![]()
21
</ItemTemplate>
22![]()
23
</asp:TemplateField>
24![]()
25
</Columns>
26![]()
27
</asp:GridView>
28![]()
29
</ItemTemplate>
30![]()
31
</asp:TemplateField>
32![]()
33
</Columns>
34![]()
35
</asp:GridView> ――――――――――――― 前台代码(grdList嵌套grdAttachList) End ―――――――――――――
前台代码说明:
1)、外层的grdList 设定了数据源: DataSourceID="sdsList",而里层的grdAttachList没有设;
2)、里层的grdAttachList设定了事件处理函数名:OnRowDataBound="grdAttach_RowDataBound" OnRowCommand="grdAttach_RowCommand",
而外层的grdList没有设定;
下面是后台代码举例:
――――――――――――― 后台代码(grdList嵌套grdAttachList) Start ―――――――――――――
![]()
后台代码
1![]()
Protected Sub Page_Lord()Sub Page_Lord(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
2![]()
3
'// SqlDataSource設定
4![]()
5
sdsList.ProviderName = 你的ProviderName
6![]()
7
sdsList.ConnectionString = 你的ConnectionString
8![]()
9
10![]()
11
'//初回ロードイベント
12![]()
13
If Not IsPostBack Then
14![]()
15
你的处理
16![]()
17
End If
18![]()
19
20![]()
21
'// リストソーティング
22![]()
23
AddHandler grdList.Sorting, AddressOf grdList_Sorting
24![]()
25
End Sub
26![]()
27
28![]()
29![]()
Protected Sub grdList_RowDataBound()Sub grdList_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
30![]()
31
Handles grdList.RowDataBound
32![]()
33
If e.Row.RowType = DataControlRowType.DataRow Then
34![]()
35
'// ファイル付属図枚数の取得
36![]()
37
Dim cntAttach As Integer = CInt(DataBinder.Eval(e.Row.DataItem, "lastAttachCount"))
38![]()
39
'// ファイル付属図ロード
40![]()
41
If cntAttach > 0 Then
42![]()
43
Dim grd As GridView = CType(e.Row.FindControl("grdAttachList"), GridView)
44![]()
45
If grd IsNot Nothing Then Me.setAttachGridView(grd, strLastDocFileID, DocBodyType.pubDoc)
46![]()
47
End If
48![]()
49
End If
50![]()
51
End Sub
52![]()
53
54![]()
55![]()
Private Sub setAttachGridView()Sub setAttachGridView(ByVal grd As GridView, ByVal docFileID As String, ByVal docBody As DocBodyType)
56![]()
57
If grd Is Nothing Or docFileID Is Nothing Or docFileID.Trim.Equals("") Then Return
58![]()
59
60![]()
61
Dim dt As DataTable
62![]()
63
dt = 你的数据
64![]()
65
66![]()
67
grd.DataSource = dt
68![]()
69
grd.DataBind()
70![]()
71
End Sub
72![]()
73
74![]()
75![]()
Protected Sub grdAttach_RowDataBound()Sub grdAttach_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
76![]()
77
If e.Row.RowType = DataControlRowType.DataRow Then
78![]()
79
Dim strAttachID As String = DataBinder.Eval(e.Row.DataItem, "attachID").ToString
80![]()
81
Dim lbtnAttach As LinkButton = CType(e.Row.FindControl("lbtnAttach"), LinkButton)
82![]()
83
'// ファイル名ボタン設置
84![]()
85
If lbtnAttach IsNot Nothing Then
86![]()
87
lbtnAttach.CommandArgument = 你的参数
88![]()
89
你的设置
90![]()
91
ElseIF
92![]()
93
你的错误处理
94![]()
95
End If
96![]()
97
98![]()
99
End If
100![]()
101
End Sub
102![]()
103
104![]()
105![]()
Protected Sub grdAttach_RowCommand()Sub grdAttach_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
106![]()
107
If Not e.CommandName.Trim.Equals("attachDownload") Then Return
108![]()
109
110![]()
111
Dim parameter As Integer = CInt(e.CommandArgument)
112![]()
113
114![]()
115
'//ファイルダウンロード
116![]()
117
Select Case e.CommandName
118![]()
119
Case "attachDownload"
120![]()
121
Me.你的处理
122![]()
123
End Select
124![]()
125
End Sub
――――――――――――― 前台代码(grdList嵌套grdAttachList) End ―――――――――――――
以上是大致框架,添上自己的处理代码后就能实现自己的应用。
特别要说明的是:
1)、子GridView不可用SqlDataSource作数据源,只能手动设置;
2)、子GridView的OnRowDataBound和OnRowCommand事件处理好像不能用AddHandler的方法,具体原因不清楚。
以上。
2009年8月22日23:43:48