.netのrepeaterネストで使用される完全なコード


2つのテーブルのデータをすべて読み出して、2つのテーブルの関係を確立します
ds.Relation.Add();   
 
親RepeaterのItemDataBindイベントで子Repeaterを見つけます
さらに関係に基づいてサブRepeater対応のデータソースを見つけ,okにバインドする.
ページ:
<asp:Repeater   id="Repeater1"   runat="server">   
  <HeaderTemplate>   
  <table   width="500">   
  <tr   bgcolor=blue>   
  <td>      </td>   
  </tr>   
  </HeaderTemplate>   
  <FooterTemplate>   
  </table>   
  </FooterTemplate>   
  <ItemTemplate>   
  <tr   bgcolor=aaaaaa>   
  <td>   :<%#DataBinder.Eval(Container.DataItem,"username")%></td>   
  </tr>   
  <tr>   
  <td>       </td>   
  </tr>   
  <tr>   
  <td><asp:Repeater   id="Repeater2"   runat="server">   
  <ItemTemplate>   
      <%#DataBinder.Eval(Container.DataItem,"content")%><br>   
  </ItemTemplate>   
  </asp:Repeater></td>   
  </tr>   
  </ItemTemplate>   
  </asp:Repeater>   
 
 
csコード:
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 AdoDotNet_Demo
{
    ///   <summary>   
    ///   RepeaterLink1        。   
    ///   </summary>   
    public class RepeaterLink1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Repeater Repeater2;
        protected System.Web.UI.WebControls.Repeater Repeater1;

        private void Page_Load(object sender, System.EventArgs e)
        {
            //                     
            if (!Page.IsPostBack)
            {
                string strconn = "server=.;uid=sa;   pwd=;database=myweb";
                SqlConnection conn = new SqlConnection(strconn);
                SqlDataAdapter da = new SqlDataAdapter("select   *   from   userinfo", conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "user");
                SqlDataAdapter da1 = new SqlDataAdapter("select   *   from   content", conn);
                da1.Fill(ds, "content");
                ds.Relations.Add("relation1", ds.Tables["user"].Columns["UserName"], ds.Tables["content"].Columns["UserName"]);

                Repeater1.DataSource = ds.Tables["user"];
                Repeater1.DataBind();
            }
        }

        #region   Web             
        override protected void OnInit(EventArgs e)
        {
            //   
            //   CODEGEN:          ASP.NET   Web            。   
            //   
            InitializeComponent();
            base.OnInit(e);
        }

        ///   <summary>   
        ///                -                 
        ///         。   
        ///   </summary>   
        private void InitializeComponent()
        {
            this.Repeater1.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                //repeater1         
                DataRowView drv = (DataRowView)e.Item.DataItem;
                DataView dv = drv.CreateChildView("relation1");

                //       repeater2       

                //      repeater2   
                System.Web.UI.WebControls.Repeater r = (System.Web.UI.WebControls.Repeater)e.Item.FindControl("Repeater2");
                r.DataSource = dv;
                r.DataBind();
            }
        }
    }
}