上自己申请的留言薄看了一下,有人说非常漂亮,问是怎么做的,其实非常简单,无非就是三大操作,难点就在于如何实现取出对应留言的回复(其实也不算难),而解决这个可以用嵌套模板来实现,巧妙点地话,连这个都可以不用做.
所谓嵌套模板就是在一个模板中加入另一个模板,如
1
<
asp:Repeater ID
=
"
parent
"
Runat
=
"
server
"
>
2 < ItemTemplate >
3 编号: <% # DataBinder.Eval(Container.DataItem, " id " ) %>
4 留言: <% # DataBinder.Eval(Container.DataItem, " content " ) %>
5 < br >
6 < asp:Repeater ID = " child " Runat = " server " >
7 < ItemTemplate >
8 回复: <% # DataBinder.Eval(Container.DataItem, " reply " ) %>< br >
9 </ ItemTemplate >
10 </ asp:Repeater >
11 </ ItemTemplate >
12 </ asp:Repeater >
2 < ItemTemplate >
3 编号: <% # DataBinder.Eval(Container.DataItem, " id " ) %>
4 留言: <% # DataBinder.Eval(Container.DataItem, " content " ) %>
5 < br >
6 < asp:Repeater ID = " child " Runat = " server " >
7 < ItemTemplate >
8 回复: <% # DataBinder.Eval(Container.DataItem, " reply " ) %>< br >
9 </ ItemTemplate >
10 </ asp:Repeater >
11 </ ItemTemplate >
12 </ asp:Repeater >
在后置代码Load中写入(假设父模板为parent,子模板为child)
OleDbConnection con
=
new
OleDbConnection(
@"
../gb.mdb
"
);
OleDbDataAdapter da = new OleDbDataAdapter( " select * from guestbook " ,con);
OleDbDataAdapter da2 = new OleDbDataAdapter( " select * from reply " ,con);
DataSet ds = new DataSet();
da.Fill(ds, " guestbook " );
da2.Fill(ds, " reply " );
ds.Relations.Add( " relation " ,ds.Tables[ 0 ].Columns[ " id " ],ds.Tables[ 1 ].Columns[ " id " ]);
parent.DataSource = ds.Tables[ 0 ].DefaultView;
parent.DataBind();
con.Close();
OleDbDataAdapter da = new OleDbDataAdapter( " select * from guestbook " ,con);
OleDbDataAdapter da2 = new OleDbDataAdapter( " select * from reply " ,con);
DataSet ds = new DataSet();
da.Fill(ds, " guestbook " );
da2.Fill(ds, " reply " );
ds.Relations.Add( " relation " ,ds.Tables[ 0 ].Columns[ " id " ],ds.Tables[ 1 ].Columns[ " id " ]);
parent.DataSource = ds.Tables[ 0 ].DefaultView;
parent.DataBind();
con.Close();
主要是为两个表之间添加关系,当前如果一个表就什么都不用做了.
接着写Repeater的 ItemDataBound 事件,这个很关键.
DataRowView dv
=
e.Item.DataItem
as
DataRowView;
child = e.Item.FindControl( " child " ) as Repeater;
child.DataSource = dv.CreateChildView( " relation " );
child.DataBind();
child = e.Item.FindControl( " child " ) as Repeater;
child.DataSource = dv.CreateChildView( " relation " );
child.DataBind();
通过FindControl()来查找子模板并赋数据源.
上面的思路其实可以理解为两个表间的父子关系映射到嵌套模板上去.
随手写的代码,乱七八糟的