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

2016-02-19 16:28 17 1 收藏

生活已是百般艰难,为何不努力一点。下面图老师就给大家分享使用嵌套的Repeater控件显示分级数据,希望可以让热爱学习的朋友们体会到设计的小小的乐趣。

【 tulaoshi.com - Web开发 】

  简介

  本文描述如何使用嵌套的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-1176213-46-8915238-95-7766267-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.保存并编译应用程序。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

  .在浏览器中察看修改后的页面。显示格式类似于下面的格式:

172-32-1176PS3333213-46-8915BU1032BU2075238-95-7766PC1035267-41-2394BU1111TC7777... 

  完整的代码

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

来源:https://www.tulaoshi.com/n/20160219/1612196.html

延伸阅读
  一般来说,每一个字段的内容会单独显示于DataGridView控件的一个数据行中。问题是,某些字段拥有大量文字数据,我是不是能够让该字段的内容以跨数据行的方式来显示,以便在有限的画面空间中的呈现出更完整的内容呢?答案当然是肯定的。 以图表1所示的执行画面而言,「自传」字段的内容并未单独显示于一个数据行中,而是以横跨数据行的...
标签: Web开发
使用XSL可以给XML文档添加上显示信息。 -------------------------------------------------------------------------------- 使用XSL来显示XML文档 XSL是XML的可扩展样式单语言。 XSL (全称是:可扩展的样式单语言,the eXtensible Stylesheet Language)比CSS样式单功能要强大的多。XSL的一个主要的用途就是将XML文档转换成HTML格式的文件...
不少应用程序都需要将一些数据曲线化显示,以观察数据的变化趋势,我们常用的曲线控件有不少,比如mschart,最近公司也需要一个曲线显示的东东,但是不能使用mschart,怎么办?自己做嘛,工具当然是Visual C++,语言MFC。首先我研究了一下mschart和一些网站的资料,经过3天的努力,终于完成,想把这个控件也提供给大家,大家先来看看效果吧...
Accecc 数据库: 设置 Connect 属性为 ;pwd=密码 Data1.Connect = ";pwd=密码" 修改密码: Data1.Database.NewPassword _老密码_, _新密码_
Prevayler 是一个把Java对象都保持在内存中的持久层框架,不需要数据库。Prevayler的原理是在执行数据操作前,都会记录一条操作日志。而在重启恢复的时候,将会把日志文件中记录的操作重新执行一遍,以保持和系统之前停止时的相同的状态。 Prevayler 还提供类似mysql的复制功能。可以启动多个Prevayler的实例,其中一个作为hot,其他...

经验教程

15

收藏

76
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部