Solved

Nested Repeater with an ArrayList

Posted on 2004-03-22
3
1,025 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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