ASP.NET 嵌套Repeater

來源:互聯網
上載者:User
asp.net

本來不怎麼用Repeater的,可最近要顯示留言板一樣的東西,用Grid實現不好看,Grid比較適合正規的WEB系統,對於網站類型的,還是用Repeater了,遇到需要嵌套Repeater的情況,就收藏一下,以後可能還會用到哦。
原文來自MS:http://support.microsoft.com/default.aspx?scid=kb;en-us;306154

iew products that this article applies to.
Article ID : 306154
Last Review : July 15, 2004
Revision : 4.1

This article was previously published under Q306154
On This Page
 SUMMARY
   Bind to the Parent Table
   Bind to the Child Table
   Complete Code List
     Nestedrepeater.aspx
     Nestedrepeater.aspx.cs
 REFERENCES
 APPLIES TO

SUMMARY
This article describes how to use nested Repeater controls to display hierarchical data. You can apply this concept to other list-bound controls.

 

 Back to the top

Bind to the Parent Table
1. Start Microsoft Visual Studio .NET.
2. On the File menu, point to New, and then click Project.
3. Click Visual C# Projects under Project Types, and then click ASP.NET Web Application under Templates.
4. In the Location box, delete the WebApplication#, and then type NestedRepeater. If you use the local server, leave the server name as http://localhost. The following path appears in the Location box:
http://localhost/ NestedRepeater
Click OK. 
5. In Solution Explorer, right-click the NestedRepeater project name node, point to Add, and then click Add Web Form.
6. To name the Web Form, type NestedRepeater, and click Open.
7. The new Web Form is created. It opens in Design View in the Integrated Development Environment (IDE) of Microsoft Visual Studio .NET. From the Toolbox, select the Repeater control, and then drag it to the Web Form page.
8. Change the ID property of this Repeater control to parentRepeater.
9. Switch to the HTML view for this Web Form. To do so, click the HTML tab in the lower-left corner of the Designer. The Repeater control generates the following HTML code:
<asp:Repeater id="parentRepeater" runat="server"></asp:Repeater>
     
 
10. Add the following code in the Repeater tags:
<itemtemplate>
     <b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br>
</itemtemplate>
     
After you do that, the HTML code for the Repeater is as follows:
<asp:Repeater id="parentRepeater" runat="server">
 <itemtemplate>
      <b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br>
      </itemtemplate>
</asp:Repeater>
     
 
11. In Solution Explorer, right-click NestedRepeater.aspx, and then click View Code to switch to the NestedRepeater.aspx.cs code-behind file.
12. Add the following namespace declaration to the top of the file:
using System.Data;
using System.Data.SqlClient;
     
 
13. Add the following code to the Page_Load event to create a connection to the Pubs database, and then to bind the Authors table to the Repeater control:
      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();
       }
     
NOTE: You may have to modify the database connection string as appropriate for your environment.


14. Save all of the files.
15. In Solution Explorer, right-click the NestedRepeater.aspx, and then click Set As Start Page. 
16. On the Build menu click Build Solution to compile the project.
17. View the .aspx page in the browser, and then verify that the page works thus far.

The output should appear as follows: • 172-32-1176
• 213-46-8915
• 238-95-7766
• 267-41-2394
• ...  
 


 Back to the top

Bind to the Child Table
1. In the HTML view of the NestedRepeater.aspx page, locate the following line of code:
<b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br>
      
Add the following code after this code:

<asp:repeater id="childRepeater" runat="server">
  <itemtemplate>
             <%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%><br>
  </itemtemplate>
</asp:repeater>
      
This new code adds a second Repeater control to the ItemTemplate property of the parent Repeater control. 
2. Set the DataSource property for the child Repeater control as follows:
<asp:repeater ... datasource='<%# ((DataRowView)Container.DataItem)
      .Row.GetChildRows("myrelation") %>' >
     
After you set the DataSource property for the child Repeater control, the HTML code for the two Repeater controls (parent and child) appears as follows:

<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. Add the following page directive to the top of the page:
<%@ Import Namespace="System.Data" %>
     
 
4. In the code-behind page, replace the following line in the Page_Load event
//Insert code in step 4 of the next section here.
      
with the following code:
         //Create a second DataAdapter for the Titles table.
         SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
         cmd2.Fill(ds,"titles");

         //Create the relation between the Authors and Titles tables.
         ds.Relations.Add("myrelation",
         ds.Tables["authors"].Columns["au_id"],
         ds.Tables["titles"].Columns["au_id"]);

     
This adds the Titles table to the DataSet, and then adds the relationships between the Authors and Titles tables.
 
5. Save and compile the application.
6. View the page in the browser, and then verify that the page works so far. The output should appear as follows:
172-32-1176
PS3333
213-46-8915
BU1032
BU2075
238-95-7766
PC1035
267-41-2394
BU1111
TC7777
... 


 Back to the top

Complete Code List
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);
      }
   }
}
   



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。