Solved

Nested Repeater with an ArrayList

Posted on 2004-03-22
3
1,021 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 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