绑定到父表
<script type="text/javascript">loadTOCNode(2, 'summary');</script>
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 代码:
|
10. | 控件将生成下列 HTML 代码:在 Repeater 标记中添加下列代码:
完成之后,Repeater 的 HTML 代码将如下所示:
|
11. | 控件将生成下列 HTML 代码:在"解决方案资源管理器"中,右键单击 NestedRepeater.aspx,然后单击查看代码切换到 NestedRepeater.aspx.cs 代码隐藏文件。 |
12. | 控件将生成下列 HTML 代码:将下面的名称空间声明添加到该文件的顶端:
|
13. | 控件将生成下列 HTML 代码:向 Page_Load 事件添加下列代码以创建到 Pubs 数据库的连接,然后将 Authors 表绑定到 Repeater 控件:
备注:您可能必须修改数据库连接字符串,使其适合于您的环境。 |
14. | 控件将生成下列 HTML 代码:保存所有文件。 |
15. | 控件将生成下列 HTML 代码:在生成菜单上,单击生成解决方案以编译该项目。 |
16. | 控件将生成下列 HTML 代码:在浏览器中查看 .aspx 页,并验证该页至今还在工作。输出应为如下所示:
172-32-1176
213-46-8915 238-95-7766 267-41-2394 ... |
绑定到子表
<script type="text/javascript">loadTOCNode(2, 'summary');</script>
1. | 在 NestedRepeater.aspx 页的 HTML 视图中,找到下列代码行:
在上述代码之后添加以下代码:
此新代码将第二个 Repeater 控件添加到父 Repeater 控件的 ItemTemplate 属性。 |
2. | 设置子 Repeater 控件的 DataSource 属性,如下所示:
设置了子 Repeater 控件的 DataSource 属性之后,两个 Repeater 控件(父和子)的 HTML 代码如下所示:
|
3. | 设置了子将下面的页指令添加到该页的顶端:
|
4. | 设置了子在代码隐藏页中,将 Page_Load 事件中的下列行:
替换为以下代码:
这样会将 Titles 表添加到 DataSet,然后添加 Authors 表和 Titles 表之间的关系。 |
5. | 设置了子保存并编译该应用程序。 |
6. | 设置了子在浏览器中查看该页,并验证该页至今还在工作。输出应为如下所示:
172-32-1176
PS3333 213-46-8915 BU1032 BU2075 238-95-7766 PC1035 267-41-2394 BU1111 TC7777 ... |
完整代码列表
<script type="text/javascript">loadTOCNode(2, 'summary');</script>
Nestedrepeater.aspx
<script type="text/javascript">loadTOCNode(3, 'summary');</script>
<%@ 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
<script type="text/javascript">loadTOCNode(3, 'summary');</script>
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);
}
}
}