数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

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


发布日期:2022年03月01日
 
使用嵌套的Repeater控件显示分级数据

简介

本文描述如何使用嵌套的Repeater 控件来显示分级数据 当然了你也可以将这一技术应用到其他的列表绑定控件上去比如DataGrid包含DataGridDataList包含DataList等等的组合

绑定到父表

添加一个新的Web Form 到应用程序项目中名称为Nestedrepeateraspx

从工具箱托动一个Repeater 控件到这个页面上 设定其ID 属性为 parent

切换到HTML 视图

选中下列<itemtemplate> 代码复制到Repeater 控件对应的位置注意粘贴的时候请使用粘贴为html功能这些语句包含了数据绑定语法很简单

<itemtemplate>

<b><%# DataBinderEval(ContainerDataItem au_id) %></b><br>

</itemtemplate>

打开Nestedrepeateraspxcs 这个代码分离文件降下列代码添加到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 cmd = new SqlDataAdapter(select * from authorscnn);

DataSet ds = new DataSet();

cmdFill(dsauthors);

//这里将要插入子表的数据绑定

parentDataSource = dsTables[authors];

PageDataBind();

cnnClose();

}

在文件的头部添加下面的名称空间

using SystemDataSqlClient;

根据你自己的情况修改一下连接字符串

保存并编译应用程序

在浏览器中打开这个页面输出结果类似于下面的格式

绑定到子表

在页面的HTML视图中添加下列代码其目的是增加子Repeater 控件到父Repeater的项目模板中形成嵌套

<asp:repeater id=child runat=server

<itemtemplate>

<%# DataBinderEval(ContainerDataItem [\title_id\]) %><br>

</itemtemplate>

</asp:repeater>

设置子Repeater 控件的DataSource 属性:

<asp:repeater datasource=<%# ((DataRowView)ContainerDataItem)

RowGetChildRows(myrelation) %>

在页面顶部添加下列指令(请注意是在aspx文件中):

<%@ Import Namespace=SystemData %>

cs文件中将Page_Load中的注释部分(//这里将要插入子表的数据绑定)替换成下列代码:

SqlDataAdapter cmd = new SqlDataAdapter(select * from titleauthorcnn);

cmdFill(dstitles);

dsRelationsAdd(myrelation

dsTables[authors]Columns[au_id]

dsTables[titles]Columns[au_id]);

保存并编译应用程序

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

PS

BU

BU

PC

BU

TC

完整的代码

Nestedrepeateraspx

<%@ Page Language=C# Inherits=yourprojectnamenestedrepeater %>

<%@ Import Namespace=SystemData %>

<html>

<body>

<form runat=server>

<! start parent repeater

<asp:repeater id=parent runat=server

<itemtemplate>

<b><%# DataBinderEval(ContainerDataItemau_id) %></b><br>

<! start child repeater

<asp:repeater id=child datasource=<%# ((DataRowView)ContainerDataItem)

RowGetChildRows(myrelation) %> runat=server

<itemtemplate>

<%# DataBinderEval(ContainerDataItem [\title_id\])%><br>

</itemtemplate>

</asp:repeater>

<! end child repeater

</itemtemplate>

</asp:repeater>

<! end parent repeater

</form>

</body>

</html>

Nestedrepeateraspxcs

using System;

using SystemData;

using SystemDataSqlClient;

using SystemWeb;

using SystemWebSessionState;

using SystemWebUI;

using SystemWebUIWebControls;

namespace yourprojectname

{

public class nestedrepeater : SystemWebUIPage

{

protected SystemWebUIWebControlsRepeater parent;

public nestedrepeater()

{

PageInit += new SystemEventHandler(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 cmd = new SqlDataAdapter(select * from authorscnn);

//Create and fill the DataSet

DataSet ds = new DataSet();

cmdFill(dsauthors);

//Create a second DataAdapter for the Titles table

SqlDataAdapter cmd = new SqlDataAdapter(select * from titleauthorcnn);

cmdFill(dstitles);

//Create the relation bewtween the Authors and Titles tables

dsRelationsAdd(myrelation

dsTables[authors]Columns[au_id]

dsTables[titles]Columns[au_id]);

//Bind the Authors table to the parent Repeater control and call DataBind

parentDataSource = dsTables[authors];

PageDataBind();

//Close the connection

cnnClose();

}

private void Page_Init(object sender EventArgs e)

{

InitializeComponent();

}

private void InitializeComponent()

{

thisLoad += new SystemEventHandler(thisPage_Load);

}

}

}

               

上一篇:asp.net连接Access数据库

下一篇:C#开源轻量级对象数据库NDatabase介绍[1]