Solved

Nested Repeater with an ArrayList

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

929 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

9 Experts available now in Live!

Get 1:1 Help Now