利用Repeater控件显示主-从关系数据表 (转)

 <%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%><%@ Page language="c#" Codebehind="NestedRepeater.aspx.cs" AutoEventWireup="false" Inherits="NestedRepeater.NestedRepeater" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body>
  <form id="Form1" method="post" runat="server">
   <!-- 父Repeater开始 -->
   <asp:repeater id="parentRepeater" runat="server">
    <itemtemplate>
    <hr>
     <b>
      <%# DataBinder.Eval(Container.DataItem,"au_id") %>
     </b>
     <br>
     <!-- 子Repeater开始 -->
     <asp:repeater id="childRepeater" runat="server" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>'>
      <itemtemplate>
       <%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%>
       <br>
      </itemtemplate>
     </asp:repeater>
     <!-- 子Repeater结束 -->
    </itemtemplate>
   </asp:repeater>
   <!-- 父Repeater结束 -->
  </form>
 </body>
</HTML>
----------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

namespace NestedRepeater
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class NestedRepeater : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Repeater parentRepeater;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 为Authors表创建 Connection 和 DataAdapter
   string cnnString = @"server=(local);database=pubs; uid=sa;pwd=sa;";
   SqlConnection cnn = new SqlConnection(cnnString);
   SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn);

   //创建填充 DataSet.
   DataSet ds = new DataSet();
   cmd1.Fill(ds,"authors");

   // 为Titles表创建 DataAdapter
   SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
   cmd2.Fill(ds,"titles");

   // 创建 Authors 表和 Titles 表之间的关系.
   ds.Relations.Add("myrelation",
   ds.Tables["authors"].Columns["au_id"],
   ds.Tables["titles"].Columns["au_id"]);

   // 绑定Authors到父Repeater
   parentRepeater.DataSource = ds.Tables["authors"];
   parentRepeater.DataBind();

   cnn.Close();
   cnn.Dispose();
/*
use pubs

select * from authors

select * from titleauthor

select a.au_id,b.title_id  from authors a,titleauthor b
where a.au_id=b.au_id
order by 1
 */


  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

<%@ Page language="c#" Codebehind="NestedRepeater.aspx.cs" AutoEventWireup="false" Inherits="NestedRepeater.NestedRepeater" %><%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%>
<%@ Page language="c#" Codebehind="NestedRepeater.aspx.cs" AutoEventWireup="false" Inherits="NestedRepeater.NestedRepeater" %>
posted @ 2007-02-11 11:55  RobotTech  阅读(339)  评论(0编辑  收藏  举报