转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
使用嵌套的Repeater控件显示分级数据         ★★★★

使用嵌套的Repeater控件显示分级数据

作者:闵涛 文章来源:闵涛的学习笔记 点击数:912 更新时间:2009/4/23 10:31:30
  简介

  本文描述如何使用嵌套的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.保存并编译应用程序。
  .在浏览器中察看修改后的页面。显示格式类似于下面的格式:

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);
   }
  }
}


[Web开发]ASP.NET Atlas简单控件介绍之四大控件  [Web开发]ASP.NET Atlas简单控件介绍之两个基类
[Web开发]ASP.NET Atlas对JavaScript的扩展  [Web开发]体验ASP.NET 2.0 中的异步页功能
[Web开发]ASP.NET 2.0站点登录、导航与权限管理  [Web开发]在ASP.NET 2.0中实现异常管理
[Web开发]ASP.NET Atlas ListView显示列表数据  [Web开发]ASP.NET 2.0客户端回调的实现分析
[Web开发]在ASP.NET Atlas中创建自定义Behavior  [Web开发]在ASP.NET Atlas中创建自定义Action
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台