?
Solved

How do I bind nested repeaters to lists in lists in code? see example

Posted on 2005-03-14
4
Medium Priority
?
431 Views
Last Modified: 2008-03-10
Hey

I create an arraylist with a list of objects. and binds it to a repeater like this:

for (int j=0; j<_esubjectmenuitem_eitemdescriptionlist.Count;j++)
 {
          ESubjectMenuItem_EItemDescription _esmi_eid1 =  
                                 (ESubjectMenuItem_EItemDescription)_esubjectmenuitem_eitemdescriptionlist[j];
          EItemDescription _eid = (EItemDescription)_esmi_eid1.EItemDescription;    
          _eitemdescriptionlist.Add(_eid);    
}

rptEItemDescription.DataSource = _eitemdescriptionlist;
rptEItemDescription.DataBind();

It works ok.
In my _eid - objects I have an ArrayList as a property: _eitemdescriptionsublist;
I want to bind it in the code to a nested repeater inside every instance of _eid so I can iterate through both the primary list and the nested list.
How can I do that?



0
Comment
Question by:JorgenVester
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
b1xml2 earned 1000 total points
ID: 13554792
rptEItemDescription.ItemDataBound += new RepeaterItemEventHandler(rptEItemDescription_ItemDataBound);
rptEItemDescription.DataSource = _eitemdescriptionlist;
rptEItemDescription.DataBind();



private void rptEItemDescription_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
   {
            Repeater repeater = e.Item.FindControl("rptEItemDescriptionSubList") as Repeater;
            EItemDescription data = e.Item.DataItem as EItemDescription;
            if (repeater != null && data != null)
            {
                  repeater.DataSource = data;
                  repeater.DataBind();
            }

  }
}

Assumptions:
1. We are assuming the nested repeater is called rptEItemDescriptionSubList.
2. You are going to have to change the structure of your classes...but the snippet above will have you understand that in order to use nested repeaters, you have to hook into the ItemDataBound event and at runtime and upon binding, find the nested repeater and bind to it.
0
 

Author Comment

by:JorgenVester
ID: 13556102
Just what I needed.
Thank you.

I made two changes and then it worked:

1. I hooked up the event like this in InitializeComponent() -  // maybe just a matter if taste.
this.rptEItemDescription.ItemDataBound += new RepeaterItemEventHandler(rptEItemDescription_ItemDataBound);

2. I did not have to change my classes. Only mad one small addition to get to the sublevel.
I have put a comment in the code to show where.

private void rptEItemDescription_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
   {
          Repeater repeater = e.Item.FindControl("rptEItemDescriptionSubList") as Repeater;
          EItemDescription data = e.Item.DataItem as EItemDescription;
          if (repeater != null && data != null)
          {
               repeater.DataSource = data.rptEItemDescriptionSubList; //this change was made to get to the sublevellist
               repeater.DataBind();
          }

  }
}

Just a matter of details. You solved the problem for me.


0
 

Author Comment

by:JorgenVester
ID: 13556128
I made a mistake in the code when I wrote it here. Here is the correct version:

private void rptEItemDescription_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
   {
          Repeater repeater = e.Item.FindControl("rptEItemDescriptionSubList") as Repeater;
          EItemDescription data = e.Item.DataItem as EItemDescription;
          if (repeater != null && data != null)
          {
               repeater.DataSource = data.EItemDescriptionSubList; //this change was made to get to the sublevellist - v. 2
               repeater.DataBind();
          }

  }
}

0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13562144
I am glad to have helped =)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…

593 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