使用嵌套模板(NestedRepeater|NestedDataList)
2005-05-15 17:47
447 查看
上自己申请的留言薄看了一下,有人说非常漂亮,问是怎么做的,其实非常简单,无非就是三大操作,难点就在于如何实现取出对应留言的回复(其实也不算难),而解决这个可以用嵌套模板来实现,巧妙点地话,连这个都可以不用做.
所谓嵌套模板就是在一个模板中加入另一个模板,如
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>
在后置代码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();
主要是为两个表之间添加关系,当前如果一个表就什么都不用做了.
接着写Repeater的 ItemDataBound 事件,这个很关键.
DataRowView dv = e.Item.DataItem as DataRowView;
child = e.Item.FindControl("child") as Repeater;
child.DataSource = dv.CreateChildView("relation");
child.DataBind();
通过FindControl()来查找子模板并赋数据源.
上面的思路其实可以理解为两个表间的父子关系映射到嵌套模板上去.
随手写的代码,乱七八糟的
所谓嵌套模板就是在一个模板中加入另一个模板,如
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>
在后置代码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();
主要是为两个表之间添加关系,当前如果一个表就什么都不用做了.
接着写Repeater的 ItemDataBound 事件,这个很关键.
DataRowView dv = e.Item.DataItem as DataRowView;
child = e.Item.FindControl("child") as Repeater;
child.DataSource = dv.CreateChildView("relation");
child.DataBind();
通过FindControl()来查找子模板并赋数据源.
上面的思路其实可以理解为两个表间的父子关系映射到嵌套模板上去.
随手写的代码,乱七八糟的
相关文章推荐
- 使用嵌套模板(NestedRepeater|NestedDataList)
- 在Repeater中嵌套使用Repeater,,及嵌套使用DataList
- 在使用Repeater、DataList 或 DataGrid 的模板列表时,根据不同状态,显示不同颜色
- DataList, Repeater Nested(嵌套)
- 关于GridView,DataList,Repeater控件的嵌套使用
- REPEATER控件显示数据,交替项显示;分页类的使用,Repeater控件分页;使用DataList控件绑定数据;使用DataList控件的选择模板
- datagrid,datalist,repeater的使用
- DataList嵌套DataList(使用SqlDataSource实现)
- repeater嵌套repeater(不使用DataSet)
- asp.net Repeater控件嵌套使用
- asp:Repeater 嵌套使用
- 在DataList、Repeater的HeaderTemplate和FooterTemplate模板中寻找控件FindControl
- [收集]在repeater、datalist控件中使用分页功能
- Repeater 控件嵌套使用
- ThinkPHP问题收集:模板中使用U方法时无法嵌套大括号,For标签,插入数据,新增的表字段缓存问题
- 在DataList、Repeater的HeaderTemplate和FooterTemplate模板中寻找控件FindControl
- Android Fragment 嵌套使用(Nested Fragments)
- 关于何种情况下使用DataGrid、DataList或Repeater的一些讨论
- asp.net控件用法--在Repeater中嵌套使用Repeater 【转】
- DataList编辑,更新,删除及模板的使用