Solved

Nested Repeater with an ArrayList

Posted on 2004-03-22
3
1,022 Views
Last Modified: 2012-08-13
Hello all,

Can anyone tell me if it is possible to have a nested repeater that utilizes an ArrayList or a multiDimensional ArrayList as its DataSource?? If so, could you share some code that would demonstrate how to bind the childview to the ArrayList.  
0
Comment
Question by:zozig
3 Comments
 
LVL 12

Accepted Solution

by:
esteban_felipe earned 500 total points
ID: 10653348
Hi zozig,

Here's a small sample

------------- webform1.aspx--------------------------
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication11.WebForm1" %>
<!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">
                  <asp:Repeater id="Repeater1" runat="server">
                        <ItemTemplate>
                              <p>
                                    <asp:Label Runat="server" /><br>
                                    <table>
                                          <tr>
                                                <td>First Name</td>
                                                <td>Last Name</td>
                                          </tr>
                                          <asp:Repeater Runat="server">
                                                <ItemTemplate>
                                                      <tr>
                                                            <td><asp:Label Runat="server" /></td>
                                                            <td><asp:Label Runat="server" /></td>
                                                      </tr>
                                                </ItemTemplate>
                                          </asp:Repeater>
                                    </table>
                              </p>
                        </ItemTemplate>
                  </asp:Repeater>
            </form>
      </body>
</HTML>


--------------webform.aspx.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;

namespace WebApplication11
{
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Repeater Repeater1;
            protected ArrayList baseList;
            private void Page_Load(object sender, System.EventArgs e)
            {
                  if(!Page.IsPostBack)
                  {
                        baseList = new ArrayList();
                        baseList.Add(
                              new Groups("Students",
                              new Human[] {
                                    new Human("joe","black"),
                                    new Human("john","smith"),
                                    new Human("carol","lucas")
                              }));
                        baseList.Add(
                              new Groups("Teachers",
                              new Human[] {
                                                      new Human("larry","white"),
                                                      new Human("Steve","smith"),
                                                      new Human("Lousie","lucas")
                                                }));
                        Repeater1.DataSource = baseList;
                        Repeater1.DataBind();
                  }
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </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)
            {
                  ((Label)e.Item.Controls[1]).Text = ((Groups)e.Item.DataItem).GroupName;
                  Repeater currentRepeater = ((Repeater)e.Item.Controls[3]);
                  currentRepeater.DataSource = ((Groups)e.Item.DataItem).Humans;
                  currentRepeater.ItemDataBound+=new RepeaterItemEventHandler(currentRepeater_ItemDataBound);
                  currentRepeater.DataBind();
            }

            private void currentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                  ((Label)e.Item.Controls[1]).Text = ((Human)e.Item.DataItem).FirstName;
                  ((Label)e.Item.Controls[3]).Text = ((Human)e.Item.DataItem).LastName;
            }
      }
      public class Human
      {
            public string FirstName;
            public string LastName;
            public Human(string firstname, string lastname)
            {
                  FirstName = firstname;
                  LastName = lastname;
            }
      }
      public struct Groups
      {
            public string GroupName;
            public Human[] Humans;
            public Groups(string name, Human[] humans)
            {
                  GroupName = name;
                  Humans = humans;
            }
      }
}


-----------------------------------------------------------------
Enjoy

Esteban Felipe
www.estebanf.com
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10653657



This doesn't really answer your question, but I think what you are asking is possible.  You just have to cast the object as the right type in order for it work properly.


Using a nested arraylist is pretty easy, you just have to remember that an arraylist returns an object so you have to 'cast' you inner list as an arraylist object and then pull the data from there.  


Nested ArrayLists

ArrayList listOuter = new ArrayList();
ArrayList listInner;

for(int outer=0;outer<10;outer++)
{
   listInner = new ArrayList();

   for(int inner=0;inner<5; inner++)    
        listInner[inner] = "item: " + inner;

   listOuter[outer] = listInner;
}



for(int i=0;i<10;i++)
   for(int j=0;j<5;j++)
      Console.WriteLine(   ((ArrayList)listOuter[i])[j].ToString()  );



Now if you want to set the inner arraylist as a datasource, I think you can do that by making sure your object is cast properly and that you are only pointing to ONE collection (not a collection of collections).
0
 
LVL 1

Author Comment

by:zozig
ID: 10653827
Thanks for the quick reply,

This example is awesome, it demonstrates exactly what I need to know. Thanks.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question