使用嵌套的Repeater控件
2024-07-21 02:24:16
供稿:网友
 
 
这个程序适用于:
•microsoft asp.net 
•microsoft vs.net 正式版 
简介
本文描述如何使用嵌套的repeater 控件来显示分级数据 。当然了,你也可以将这一技术应用到其他的列表绑定控件上去,比如datagrid包含datagrid,datalist包含datalist等等的组合。 
绑定到父表 
1.添加一个新的web form 到应用程序项目中,名称为nestedrepeater.aspx. 
2.从工具箱托动一个repeater 控件到这个页面上, 设定其id 属性为 parent . 
3.切换到html 视图. 
4.选中下列<itemtemplate> 代码,复制到repeater 控件对应的位置。注意,粘贴的时候请使用“粘贴为html”功能。这些语句包含了数据绑定语法,很简单。 
<itemtemplate>
<b><%# databinder.eval(container.dataitem, "au_id") %></b><br>
</itemtemplate>
5.打开nestedrepeater.aspx.cs 这个代码分离文件。降下列代码添加到page_load 事件中,其作用是建立一个到 pubs (这个数据库是sql server的演示数据库。另外在安装.net framework sdk的时候也会安装这个数据库)数据库的连接,并绑定authors 表到repeater 控件 
public void page_load()
{
   sqlconnection cnn = new sqlconnection("server=(local);database=pubs;uid=sa;pwd=;");
   sqldataadapter cmd1 = new sqldataadapter("select * from authors",cnn);
   dataset ds = new dataset();
   cmd1.fill(ds,"authors");
   //这里将要插入子表的数据绑定
   parent.datasource = ds.tables["authors"];
   page.databind();
   cnn.close();
}
6.在文件的头部添加下面的名称空间 
using system.data.sqlclient;
7.根据你自己的情况修改一下连接字符串 
8.保存并编译应用程序 
9.在浏览器中打开这个页面,输出结果类似于下面的格式 
172-32-1176 
213-46-8915 
238-95-7766 
267-41-2394 
... 
绑定到子表 
1.在页面的html视图中,添加下列代码。其目的是增加子repeater 控件到父repeater的项目模板中,形成嵌套。 
<asp:repeater id="child" runat="server">
   <itemtemplate>
   <%# databinder.eval(container.dataitem, "[/"title_id/"]") %><br>
   </itemtemplate>
</asp:repeater>
2.设置子repeater 控件的datasource 属性: 
<asp:repeater ... datasource="<%# ((datarowview)container.dataitem)
.row.getchildrows("myrelation") %>">
3.在页面顶部添加下列指令(请注意,是在.aspx文件中): 
<%@ import namespace="system.data" %>
在.cs文件中,将page_load中的注释部分(//这里将要插入子表的数据绑定)替换成下列代码: 
sqldataadapter cmd2 = new sqldataadapter("select * from titleauthor",cnn);
cmd2.fill(ds,"titles");
ds.relations.add("myrelation",
ds.tables["authors"].columns["au_id"],
ds.tables["titles"].columns["au_id"]);
4.保存并编译应用程序. 
5.在浏览器中察看修改后的页面。显示格式类似于下面的格式: 
172-32-1176 
ps3333 
213-46-8915 
bu1032 
bu2075 
238-95-7766 
pc1035 
267-41-2394 
bu1111 
tc7777 
... 
完整的代码 
nestedrepeater.aspx 
<%@ page language=c# inherits="yourprojectname.nestedrepeater" %>
<%@ import namespace="system.data" %>
<html>
<body>
<form runat=server>
<!-- start parent repeater -->
<asp:repeater id="parent" runat="server">
   <itemtemplate>
      <b><%# databinder.eval(container.dataitem,"au_id") %></b><br>
      <!-- start child repeater -->
      <asp:repeater id="child" datasource="<%# ((datarowview)container.dataitem)
      .row.getchildrows("myrelation") %>" runat="server">
         <itemtemplate>
            <%# databinder.eval(container.dataitem, "[/"title_id/"]")%><br>
         </itemtemplate>
      </asp:repeater>
      <!-- end child repeater -->
   </itemtemplate>
</asp:repeater>
<!-- end parent repeater -->
</form>
</body>
</html>
nestedrepeater.aspx.cs 
using system;
using system.data;
using system.data.sqlclient;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
namespace yourprojectname
{
   public class nestedrepeater : system.web.ui.page
   {
      protected system.web.ui.webcontrols.repeater parent;
      public nestedrepeater()
      {
         page.init += new system.eventhandler(page_init);
      }
      public void page_load(object sender, eventargs e)
      {
         //create the connection and dataadapter for the authors table.
         sqlconnection cnn = new sqlconnection("server=(local);database=pubs;uid=sa;pwd=;");
         sqldataadapter cmd1 = new sqldataadapter("select * from authors",cnn);
         //create and fill the dataset.
         dataset ds = new dataset();
         cmd1.fill(ds,"authors");
         //create a second dataadapter for the titles table.
         sqldataadapter cmd2 = new sqldataadapter("select * from titleauthor",cnn);
         cmd2.fill(ds,"titles");
         //create the relation bewtween the authors and titles tables.
         ds.relations.add("myrelation",
         ds.tables["authors"].columns["au_id"],
         ds.tables["titles"].columns["au_id"]);
         //bind the authors table to the parent repeater control, and call databind.
         parent.datasource = ds.tables["authors"];
         page.databind();
         //close the connection.
         cnn.close();
      }
      private void page_init(object sender, eventargs e)
      {
         initializecomponent();
      }
      private void initializecomponent()
      {    
         this.load += new system.eventhandler(this.page_load);
      }
   }
}