ASP
  项目合作 | 广告服务 | 联系我们 | 友情链接 | 链接代码 | 短信服务 | 给我留言 | 返回首页                    
设为首页
加入收藏
ASP技术 | ASP.NET技术 | JSP技术 | Servlet技术 | PHP技术 | JS技术 | C#技术 | Java技术 | B/S应用 | B/S论坛 | 下载区
 
 自定义DataGrid分页设置
 ASP.NET中使用Caching
 一个文件上传的类
 ASP.NET中在线用户统计
 网站间共享数据的WebService
 ASP.NET里的事务处理
 VS.NET下web项目源代码管理
 用TreeView实现树菜单
 在DataGrid中创建一个弹出式窗.
 有关DataGrid显示数据的问题
 让Calendar在页面调用时才显示.
 在web.config中建立数据库连接.
 实时天气及24小时天气预报
 数据库中图片存储及读取
 如何在窗体和窗体之间传送数据.
 利用ASP.NET来访问Excel文档
 使用嵌套的Repeater控件
 用asp.net画饼图
 实现DataList控件的分页
 用ASP.NET加密口令
 使用RangeValidator
 C#做的ASP.NET登錄篇
 ASP.NET图象处理详解
 在网页中动态的生成一个图片
 检测含有中文字符串的实际长度.
 用ASP.NET和XML做的新闻系统
 如何在asp.net中操作文件
 ASP.NET验证控件祥解
 ASP.NET中的事务处理和异常处理.
 ASP.NET上传文件的实例
 在ASP.NET中访问SQL Server
 ASP.NET安全身份验证的实现
 ASP.NET中密码保护
 在ASP.NET中使用.NET组件
 实现Web文件的上传
 asp.net实现pop功能
 ASP.NET创建文件并写入内容
 在ASP.NET中动态生成图形
 ASP.NET中文显示之两种解决方法.
 ASP.NET验证控件详解
 在ASP.NET中使用.NET组件
 资料验证的asp.net程序
 一个完整的案例
 在ASP+的Form中检查填写是否正.
 增加修改删除一个数据
 个性化的分页实现
 用ASP.NET识别浏览器
 DataGrid学习七
 DataGrid学习六
 DataGrid学习五
 DataGrid学习四
 DataGrid学习三
 DataGrid学习二
 DataGrid学习一
 DataTable控件的使用
 RegularExpressionValidator
 ViewState初探
 ASP.Net的Session
 ASP.Net的Application
 AdRotator控件的使用
 ASP.Net的Cookie实现
 XML、DataSet、DataGrid结合二.
 XML、DataSet、DataGrid结合一.
 ASP.NET中的Web Controls
 ASP.NET程序数组功能调用
 ASP.NET与ASP的不同
 ASP.net发送Email
 ASP.NET、JSP及PHP之间的抉
 

使用嵌套的Repeater控件

源作者:wincheer                   人气:3597

这个程序适用于:
•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);
      }
   }
}


参考
更详细的信息,参考Microsoft .NET Framework SDK中下列文章:
在表之间增加关系
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconaddingrelationshipbetweentwotables.asp
在表之间导航关系
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconnavigatingrelationshipbetweentwotables.asp
Repeater Web 服务器控件
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconrepeaterwebcontrol.asp

如有疑问,请赐电邮:webmaster@chinabs.net  OICQ:28194826
技术开发:深圳市百越软件工作室
中国BS网版权所有     Copyright chinabs.net