前台
代码
1
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
Debug
=
"
true
"
CodeFile
=
"
GridviewNested.aspx.cs
"
2 Inherits = " 多层嵌套_GridviewNested " %>
3
4 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
5 < html xmlns = " http://www.w3.org/1999/xhtml " >
6 < head runat = " server " >
7 < title > Gridview Nested </ title >
8 < link href = " ~/CSS/Gridview.css " rel = " stylesheet " type = " text/css " />
9 </ head >
10 < body >
11 < form id = " form1 " runat = " server " >
12 < div id = " container " >
13 < asp:GridView ID = " ParentGridView " BorderColor = " Black " OnRowDataBound = " ParentGridView_RowDataBound "
14 runat = " server " AutoGenerateColumns = " False " Font - Size = " 12px " Width = " 530px " AllowSorting = " True "
15 OnRowEditing = " ParentGridView_RowEditing " OnRowCancelingEdit = " ParentGridView_RowCancelingEdit " >
16 < Columns >
17 < asp:BoundField DataField = " CategoryID " HeaderText = " 编号 " ReadOnly = " True " />
18 < asp:BoundField DataField = " CategoryName " HeaderText = " 类别 " ReadOnly = " True " />
19 < asp:TemplateField HeaderText = " 查看详情 " >
20 < ItemTemplate >
21 < asp:Button ID = " ViewChild_Button " runat = " server " Text = " Details " CommandName = " Edit " />
22 </ ItemTemplate >
23 < EditItemTemplate >
24 < asp:Button ID = " CancelChild_Button " runat = " server " Text = " Cancel " CommandName = " Cancel " />
25 < asp:GridView ID = " ChildGridView " runat = " server " AllowPaging = " True " PageSize = " 3 " AutoGenerateColumns = " False "
26 OnRowEditing = " ChildGridView_OnRowEditing " BorderColor = " Black " OnRowDataBound = " ChildGridView_RowDataBound "
27 DataKeyNames = " ProductID " DataSourceID = " AccessDataSource1 " Width = " 241px " >
28 < Columns >
29 < asp:BoundField DataField = " ProductName " HeaderText = " 名称 " ReadOnly = " True " SortExpression = " ProductName " />
30 < asp:TemplateField HeaderText = " 查看详情 " >
31 < ItemTemplate >
32 < asp:Button ID = " ViewGrantChild_Button " runat = " server " Text = " Details " CommandName = " Edit " />
33 </ ItemTemplate >
34 < EditItemTemplate >
35 < asp:Button ID = " CancelGrantChild_Button " runat = " server " Text = " Cancel " CommandName = " Cancel " />
36 < asp:GridView ID = " GrantChildGridView " runat = " server " AllowPaging = " True " PageSize = " 3 "
37 AutoGenerateColumns = " False " BorderColor = " Black " OnRowDataBound = " ChildGridView_RowDataBound "
38 DataKeyNames = " ID " Width = " 331px " DataSourceID = " AccessDataSource1 " >
39 < Columns >
40 < asp:BoundField DataField = " OrderID " HeaderText = " 订单号 " ReadOnly = " true " SortExpression = " OrderID "
41 ItemStyle - Width = " 100px " />
42 < asp:BoundField DataField = " OrderMoney " HeaderText = " 金额 " ReadOnly = " true " SortExpression = " OrderMoney "
43 ItemStyle - Width = " 80px " />
44 < asp:BoundField DataField = " OrderState " HeaderText = " 状态 " ReadOnly = " true " SortExpression = " OrderState "
45 ItemStyle - Width = " 80px " />
46 </ Columns >
47 < HeaderStyle BackColor = " Azure " Font - Size = " 12px " HorizontalAlign = " Center " />
48 < RowStyle HorizontalAlign = " Center " />
49 < PagerStyle HorizontalAlign = " Center " />
50 </ asp:GridView >
51 < asp:AccessDataSource ID = " AccessDataSource1 " runat = " server " DataFile = " ~/App_Data/Example.mdb "
52 SelectCommand = " SELECT * FROM [Orders] WHERE ([ProductID] = ?) " >
53 < SelectParameters >
54 < asp:SessionParameter Name = " ProductID " SessionField = " sProductID " Type = " Int32 " />
55 </ SelectParameters >
56 </ asp:AccessDataSource >
57 </ EditItemTemplate >
58 </ asp:TemplateField >
59 </ Columns >
60 < HeaderStyle BackColor = " Azure " Font - Size = " 12px " HorizontalAlign = " Center " />
61 < RowStyle HorizontalAlign = " Center " />
62 < PagerStyle HorizontalAlign = " Center " />
63 </ asp:GridView >
64 < asp:AccessDataSource ID = " AccessDataSource1 " runat = " server " DataFile = " ~/App_Data/Example.mdb "
65 SelectCommand = " SELECT * FROM [Products] WHERE ([CategoryID] = ?) " >
66 < SelectParameters >
67 < asp:SessionParameter Name = " CategoryID " SessionField = " sCategoryID " Type = " String " />
68 </ SelectParameters >
69 </ asp:AccessDataSource >
70 </ EditItemTemplate >
71 </ asp:TemplateField >
72 </ Columns >
73 < HeaderStyle BackColor = " Azure " Font - Size = " 12px " HorizontalAlign = " Center " />
74 < RowStyle HorizontalAlign = " Center " />
75 < PagerStyle HorizontalAlign = " Center " />
76 </ asp:GridView >
77 </ div >
78 </ form >
79 </ body >
80 </ html >
81
2 Inherits = " 多层嵌套_GridviewNested " %>
3
4 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
5 < html xmlns = " http://www.w3.org/1999/xhtml " >
6 < head runat = " server " >
7 < title > Gridview Nested </ title >
8 < link href = " ~/CSS/Gridview.css " rel = " stylesheet " type = " text/css " />
9 </ head >
10 < body >
11 < form id = " form1 " runat = " server " >
12 < div id = " container " >
13 < asp:GridView ID = " ParentGridView " BorderColor = " Black " OnRowDataBound = " ParentGridView_RowDataBound "
14 runat = " server " AutoGenerateColumns = " False " Font - Size = " 12px " Width = " 530px " AllowSorting = " True "
15 OnRowEditing = " ParentGridView_RowEditing " OnRowCancelingEdit = " ParentGridView_RowCancelingEdit " >
16 < Columns >
17 < asp:BoundField DataField = " CategoryID " HeaderText = " 编号 " ReadOnly = " True " />
18 < asp:BoundField DataField = " CategoryName " HeaderText = " 类别 " ReadOnly = " True " />
19 < asp:TemplateField HeaderText = " 查看详情 " >
20 < ItemTemplate >
21 < asp:Button ID = " ViewChild_Button " runat = " server " Text = " Details " CommandName = " Edit " />
22 </ ItemTemplate >
23 < EditItemTemplate >
24 < asp:Button ID = " CancelChild_Button " runat = " server " Text = " Cancel " CommandName = " Cancel " />
25 < asp:GridView ID = " ChildGridView " runat = " server " AllowPaging = " True " PageSize = " 3 " AutoGenerateColumns = " False "
26 OnRowEditing = " ChildGridView_OnRowEditing " BorderColor = " Black " OnRowDataBound = " ChildGridView_RowDataBound "
27 DataKeyNames = " ProductID " DataSourceID = " AccessDataSource1 " Width = " 241px " >
28 < Columns >
29 < asp:BoundField DataField = " ProductName " HeaderText = " 名称 " ReadOnly = " True " SortExpression = " ProductName " />
30 < asp:TemplateField HeaderText = " 查看详情 " >
31 < ItemTemplate >
32 < asp:Button ID = " ViewGrantChild_Button " runat = " server " Text = " Details " CommandName = " Edit " />
33 </ ItemTemplate >
34 < EditItemTemplate >
35 < asp:Button ID = " CancelGrantChild_Button " runat = " server " Text = " Cancel " CommandName = " Cancel " />
36 < asp:GridView ID = " GrantChildGridView " runat = " server " AllowPaging = " True " PageSize = " 3 "
37 AutoGenerateColumns = " False " BorderColor = " Black " OnRowDataBound = " ChildGridView_RowDataBound "
38 DataKeyNames = " ID " Width = " 331px " DataSourceID = " AccessDataSource1 " >
39 < Columns >
40 < asp:BoundField DataField = " OrderID " HeaderText = " 订单号 " ReadOnly = " true " SortExpression = " OrderID "
41 ItemStyle - Width = " 100px " />
42 < asp:BoundField DataField = " OrderMoney " HeaderText = " 金额 " ReadOnly = " true " SortExpression = " OrderMoney "
43 ItemStyle - Width = " 80px " />
44 < asp:BoundField DataField = " OrderState " HeaderText = " 状态 " ReadOnly = " true " SortExpression = " OrderState "
45 ItemStyle - Width = " 80px " />
46 </ Columns >
47 < HeaderStyle BackColor = " Azure " Font - Size = " 12px " HorizontalAlign = " Center " />
48 < RowStyle HorizontalAlign = " Center " />
49 < PagerStyle HorizontalAlign = " Center " />
50 </ asp:GridView >
51 < asp:AccessDataSource ID = " AccessDataSource1 " runat = " server " DataFile = " ~/App_Data/Example.mdb "
52 SelectCommand = " SELECT * FROM [Orders] WHERE ([ProductID] = ?) " >
53 < SelectParameters >
54 < asp:SessionParameter Name = " ProductID " SessionField = " sProductID " Type = " Int32 " />
55 </ SelectParameters >
56 </ asp:AccessDataSource >
57 </ EditItemTemplate >
58 </ asp:TemplateField >
59 </ Columns >
60 < HeaderStyle BackColor = " Azure " Font - Size = " 12px " HorizontalAlign = " Center " />
61 < RowStyle HorizontalAlign = " Center " />
62 < PagerStyle HorizontalAlign = " Center " />
63 </ asp:GridView >
64 < asp:AccessDataSource ID = " AccessDataSource1 " runat = " server " DataFile = " ~/App_Data/Example.mdb "
65 SelectCommand = " SELECT * FROM [Products] WHERE ([CategoryID] = ?) " >
66 < SelectParameters >
67 < asp:SessionParameter Name = " CategoryID " SessionField = " sCategoryID " Type = " String " />
68 </ SelectParameters >
69 </ asp:AccessDataSource >
70 </ EditItemTemplate >
71 </ asp:TemplateField >
72 </ Columns >
73 < HeaderStyle BackColor = " Azure " Font - Size = " 12px " HorizontalAlign = " Center " />
74 < RowStyle HorizontalAlign = " Center " />
75 < PagerStyle HorizontalAlign = " Center " />
76 </ asp:GridView >
77 </ div >
78 </ form >
79 </ body >
80 </ html >
81
后台
代码
1
public
partial
class
多层嵌套_GridviewNested : System.Web.UI.Page
2 {
3 protected void Page_Load( object sender, EventArgs e)
4 {
5 if ( ! IsPostBack)
6 {
7 bindParent();
8 }
9 }
10 /// <summary>
11 /// 数据绑定
12 /// </summary>
13 public void bindParent()
14 {
15 string sqlStr = " select * from Categories " ;
16 DataSet myds = Common.dataSet(sqlStr);
17 ParentGridView.DataSource = myds;
18 ParentGridView.DataKeyNames = new string [] { " CategoryID " };
19 ParentGridView.DataBind();
20 }
21
22 /// <summary>
23 /// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。
24 /// </summary>
25 /// <param name="sender"></param>
26 /// <param name="e"></param>
27 protected void ParentGridView_RowDataBound( object sender, GridViewRowEventArgs e)
28 {
29 foreach (TableCell tc in e.Row.Cells)
30 {
31 tc.Attributes[ " style " ] = " border-color:Black " ;
32 }
33 if (e.Row.RowIndex != - 1 )
34 {
35 int id = ParentGridView.PageIndex * ParentGridView.PageSize + e.Row.RowIndex + 1 ;
36 e.Row.Cells[ 0 ].Text = id.ToString();
37 }
38 }
39 protected void ParentGridView_RowEditing( object sender, GridViewEditEventArgs e)
40 {
41 int parent_index = e.NewEditIndex;
42 ParentGridView.EditIndex = parent_index;
43 bindParent();
44 Session[ " sCategoryID " ] = Convert.ToInt32(ParentGridView.DataKeys[parent_index].Value);
45 Session[ " sParentGridViewIndex " ] = parent_index;
46 }
47 protected void ChildGridView_OnRowEditing( object sender, GridViewEditEventArgs e)
48 {
49 int parent_index = ( int )Session[ " sParentGridViewIndex " ];
50 GridViewRow parent_row = ParentGridView.Rows[parent_index];
51 GridView ChildGridView = (GridView)parent_row.FindControl( " ChildGridView " );
52 int child_index = e.NewEditIndex;
53 ChildGridView.EditIndex = child_index;
54 Session[ " sProductID " ] = Convert.ToInt32(ChildGridView.DataKeys[child_index].Value);
55 }
56 protected void ChildGridView_RowDataBound( object sender, GridViewRowEventArgs e)
57 {
58 foreach (TableCell tc in e.Row.Cells)
59 {
60 tc.Attributes[ " style " ] = " border-color:Black " ;
61 }
62
63 }
64 protected void GrandChildGridView_RowDataBound( object sender, GridViewRowEventArgs e)
65 {
66 foreach (TableCell tc in e.Row.Cells)
67 {
68 tc.Attributes[ " style " ] = " border-color:Black " ;
69 }
70 }
71 protected void ParentGridView_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e)
72 {
73 ParentGridView.EditIndex = - 1 ;
74 bindParent();
75 }
76 }
77
2 {
3 protected void Page_Load( object sender, EventArgs e)
4 {
5 if ( ! IsPostBack)
6 {
7 bindParent();
8 }
9 }
10 /// <summary>
11 /// 数据绑定
12 /// </summary>
13 public void bindParent()
14 {
15 string sqlStr = " select * from Categories " ;
16 DataSet myds = Common.dataSet(sqlStr);
17 ParentGridView.DataSource = myds;
18 ParentGridView.DataKeyNames = new string [] { " CategoryID " };
19 ParentGridView.DataBind();
20 }
21
22 /// <summary>
23 /// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。
24 /// </summary>
25 /// <param name="sender"></param>
26 /// <param name="e"></param>
27 protected void ParentGridView_RowDataBound( object sender, GridViewRowEventArgs e)
28 {
29 foreach (TableCell tc in e.Row.Cells)
30 {
31 tc.Attributes[ " style " ] = " border-color:Black " ;
32 }
33 if (e.Row.RowIndex != - 1 )
34 {
35 int id = ParentGridView.PageIndex * ParentGridView.PageSize + e.Row.RowIndex + 1 ;
36 e.Row.Cells[ 0 ].Text = id.ToString();
37 }
38 }
39 protected void ParentGridView_RowEditing( object sender, GridViewEditEventArgs e)
40 {
41 int parent_index = e.NewEditIndex;
42 ParentGridView.EditIndex = parent_index;
43 bindParent();
44 Session[ " sCategoryID " ] = Convert.ToInt32(ParentGridView.DataKeys[parent_index].Value);
45 Session[ " sParentGridViewIndex " ] = parent_index;
46 }
47 protected void ChildGridView_OnRowEditing( object sender, GridViewEditEventArgs e)
48 {
49 int parent_index = ( int )Session[ " sParentGridViewIndex " ];
50 GridViewRow parent_row = ParentGridView.Rows[parent_index];
51 GridView ChildGridView = (GridView)parent_row.FindControl( " ChildGridView " );
52 int child_index = e.NewEditIndex;
53 ChildGridView.EditIndex = child_index;
54 Session[ " sProductID " ] = Convert.ToInt32(ChildGridView.DataKeys[child_index].Value);
55 }
56 protected void ChildGridView_RowDataBound( object sender, GridViewRowEventArgs e)
57 {
58 foreach (TableCell tc in e.Row.Cells)
59 {
60 tc.Attributes[ " style " ] = " border-color:Black " ;
61 }
62
63 }
64 protected void GrandChildGridView_RowDataBound( object sender, GridViewRowEventArgs e)
65 {
66 foreach (TableCell tc in e.Row.Cells)
67 {
68 tc.Attributes[ " style " ] = " border-color:Black " ;
69 }
70 }
71 protected void ParentGridView_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e)
72 {
73 ParentGridView.EditIndex = - 1 ;
74 bindParent();
75 }
76 }
77