Solved

Nested Repeater with an ArrayList

Posted on 2004-03-22
3
1,019 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
Comment Utility
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_
Comment Utility



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
Comment Utility
Thanks for the quick reply,

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now