使用嵌套 Repeater 控件和 Visual C# .Net 显示分层数据
2005-08-31 16:57
796 查看
HOW TO:使用嵌套 Repeater 控件和 Visual C# .Net 显示分层数据
察看本文应用于的产品文章编号 | : | 306154 |
最后修改 | : | 2002年9月27日 |
修订 | : | 1.0 |
概要 | |||
绑定到父表 | |||
绑定到子表 | |||
完整代码列表 | |||
Nestedrepeater.aspx | |||
Nestedrepeater.aspx.cs | |||
参考 | |||
这篇文章中的信息适用于: |
概要
本文介绍如何使用嵌套 Repeater 控件显示分层数据。可以将此概念应用到其他列表绑定控件。回到顶端 |
绑定到父表
1. | 启动 Microsoft Visual Studio .NET。 |
2. | 在文件菜单上,指向新建,然后单击项目。 |
3. | 单击项目类型下的 Visual C# 项目,然后单击模板下面的"ASP.NET Web 应用程序"。 |
4. | 在位置框中,删除 WebApplication # ,然后键入 NestedRepeater。如果您使用的是本地服务器,请保留服务器名称 http://localhost 不变。下列路径将显示在位置框中: http://localhost/ NestedRepeater单击确定。 |
5. | 在解决方案资源管理器中,右键单击 NestedRepeater 项目名称节点,指向添加,然后单击添加 Web 窗体。 |
6. | 若要重命名 Web 窗体,请键入 NestedRepeater,然后单击打开。 |
7. | 创建了新的 Web 窗体。它将在 Microsoft Visual Studio .NET 集成开发环境 (IDE) 的设计视图中打开。从工具箱,选择 Repeater 控件,然后将其拖到 Web 窗体页面。 |
8. | 将此 Repeater 控件的 ID 属性更改为"parentRepeater"。 |
9. | 切换到 Web 窗体的 HTML 视图。为此,请单击设计器左下角的 HTML 选项卡。Repeater 控件将生成下列 HTML 代码: <asp:Repeater id="parentRepeater" runat="server"></asp:Repeater> |
10. | 控件将生成下列 HTML 代码:在 Repeater 标记中添加下列代码: <itemtemplate> <b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br> </itemtemplate>完成之后,Repeater 的 HTML 代码将如下所示: <asp:Repeater id="parentRepeater" runat="server"> |
11. | 控件将生成下列 HTML 代码:在"解决方案资源管理器"中,右键单击 NestedRepeater.aspx,然后单击查看代码切换到 NestedRepeater.aspx.cs 代码隐藏文件。 |
12. | 控件将生成下列 HTML 代码:将下面的名称空间声明添加到该文件的顶端: using System.Data; using System.Data.SqlClient; |
13. | 控件将生成下列 HTML 代码:向 Page_Load 事件添加下列代码以创建到 Pubs 数据库的连接,然后将 Authors 表绑定到 Repeater 控件: 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; Integrated Security=SSPI"); SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn); //Create and fill the DataSet. DataSet ds = new DataSet(); cmd1.Fill(ds,"authors"); //Insert code in step 4 of the next section here. //Bind the Authors table to the parent Repeater control, and call DataBind. parentRepeater.DataSource = ds.Tables["authors"]; Page.DataBind(); //Close the connection. cnn.Close(); }备注:您可能必须修改数据库连接字符串,使其适合于您的环境。 |
14. | 控件将生成下列 HTML 代码:保存所有文件。 |
15. | 控件将生成下列 HTML 代码:在生成菜单上,单击生成解决方案以编译该项目。 |
16. | 控件将生成下列 HTML 代码:在浏览器中查看 .aspx 页,并验证该页至今还在工作。输出应为如下所示: 172-32-1176 213-46-8915 238-95-7766 267-41-2394 ... |
回到顶端 |
绑定到子表
1. | 在 NestedRepeater.aspx 页的 HTML 视图中,找到下列代码行: <b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br>在上述代码之后添加以下代码: <asp:repeater id="childRepeater" runat="server"> <itemtemplate> <%# DataBinder.Eval(Container.DataItem, "[/"title_id/"]")%><br> </itemtemplate> </asp:repeater>此新代码将第二个 Repeater 控件添加到父 Repeater 控件的 ItemTemplate 属性。 |
2. | 设置子 Repeater 控件的 DataSource 属性,如下所示: <asp:repeater ... datasource='<%# ((DataRowView)Container.DataItem) .Row.GetChildRows("myrelation") %>'>设置了子 Repeater 控件的 DataSource 属性之后,两个 Repeater 控件(父和子)的 HTML 代码如下所示: <asp:Repeater id="parentRepeater" runat="server"> <itemtemplate> "b"; <%# DataBinder.Eval(Container.DataItem, "au_id") %> </b> <br> <asp:repeater id="childRepeater" runat="server"> datasource='<%# ((DataRowView)Container.DataItem) .Row.GetChildRows("myrelation") %>' > <itemtemplate> <%# DataBinder.Eval(Container.DataItem, "[/"title_id/"]")%><br> </itemtemplate> </asp:Repeater> </itemtemplate> </asp:Repeater> |
3. | 设置了子将下面的页指令添加到该页的顶端: <%@ Import Namespace="System.Data" %> |
4. | 设置了子在代码隐藏页中,将 Page_Load 事件中的下列行: //Insert code in step 4 of the next section here.替换为以下代码: //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"]);这样会将 Titles 表添加到 DataSet,然后添加 Authors 表和 Titles 表之间的关系。 |
5. | 设置了子保存并编译该应用程序。 |
6. | 设置了子在浏览器中查看该页,并验证该页至今还在工作。输出应为如下所示: 172-32-1176 PS3333 213-46-8915 BU1032 BU2075 238-95-7766 PC1035 267-41-2394 BU1111 TC7777 ... |
回到顶端 |
完整代码列表
Nestedrepeater.aspx
<%@ Page language="c#" Codebehind="NestedRepeater.aspx.cs" AutoEventWireup="false" Inherits="NestedRepeater.NestedRepeater" %> <%@ Import Namespace="System.Data" %> <html> <body> <form runat=server> <!-- start parent repeater --> <asp:repeater id="parentRepeater" runat="server"> <itemtemplate> <b><%# DataBinder.Eval(Container.DataItem,"au_id") %></b><br> <!-- start child repeater --> <asp:repeater id="childRepeater" 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 NestedRepeater
{
public class nestedrepeater :System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater parentRepeater;
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; Integrated Security=SSPI ;");
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.
parentRepeater.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 框架软件开发工具包 (SDK) 中的下列主题: Adding a Relationship between Tables(添加表之间的关系)http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaddingrelationshipbetweentwotables.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaddingrelationshipbetweentwotables.asp)
Navigating a Relationship between Tables(浏览表之间的关系)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnavigatingrelationshipbetweentwotables.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnavigatingrelationshipbetweentwotables.asp)
Repeater Web Server Control(Repeater Web 服务器控件)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconrepeaterwebcontrol.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconrepeaterwebcontrol.asp)
回到顶端 |
相关文章推荐
- HOW TO:使用嵌套 Repeater 控件和 Visual C# .Net 显示分层数据
- 使用嵌套 Repeater 控件和 Visual C# .Net 显示分层数据
- 使用嵌套 Repeater 控件和 Visual C# .Net 显示分层数据
- HOW TO:使用嵌套 Repeater 控件和 Visual Basic .NET显示分层数据
- 使用嵌套的Repeater控件显示分级数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 使用嵌套的Repeater控件显示分级数据
- 使用嵌套的Repeater控件显示分级数据(转)
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 使用嵌套的Repeater控件显示分级数据
- 使用嵌套的Repeater 控件来显示分级数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 使用嵌套的Repeater控件显示分级数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 使用嵌套的Repeater控件显示分级数据
- 使用嵌套的Repeater控件显示分级数据
- 使用嵌套的Repeater控件显示分级数据