nesting repeaters in asp.net

Last post 05-26-2008, 4:43 AM by patelmiteshb. 6 replies.
Sort Posts: Previous Next
  •  06-27-2006, 11:02 AM Post number 907

    nesting repeaters in asp.net

    First post to the forum eh? Oh well, here goes.

    By way of teaching myself VB and ASP.NET, I am trying to write a simple Web app that allows me to browse my CD collection. I have a list of CDs and a list of tracks on each CD. What I want to be able to do is display a table with the names of all the CDs and next to each name I want a nested table that lists all the tracks. I've been reading around but can't work out how to do it.

    Can anyone point me in the right direction. I'm using ASP.NET 1.1.

    Thanks,

    Richard.

  •  06-27-2006, 11:33 PM Post number 925 in reply to post number 907

    • Damon is not online. Last active: 11-26-2008, 11:06 AM Damon
    • Top 10 Contributor
    • Joined on 06-26-2006
    • Dallas, TX
    • Acorn Archimedes

    Re: nesting repeaters in asp.net

    Nesting repeaters is a lot of fun.  And by fun I mean tedious, and by "a lot" I mean work.  Your outter repeater is actually on the page itself, so you can see that repeater pretty easily in your code.  We'll call it RepeaterA for the the time being.  But your page has no idea about the nested repeater because the nested repeater only exists within the context of a single "row" of RepeaterA. 

    Huh?  Yeah, very confusing.  Here's how it works.  You have a list of CDs.  When you bind that list to the repeater, it creates a row for each CD.  When it creates a row, it actually creates a new nested repeater where you want to put the track information.  So you have to get a reference to that nested repeater, and you can do that in the ItemDataBound event of RepeaterA.  Here's an example:

    private void RepeaterA_ItemDataBound(object sender,
         System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
         if(e.Item.ItemType == ListItemType.Item ||
                e.Item.ItemType == ListItemType.AlternatingItem)
         {
              Repeater NestedRepeater = (Repeater)e.Item.FindControl("NestedRepeater");
              NestedRepeater.DataSource = GetTrackInfo((CdInfo)e.Item.DataItem);
              NestedRepeater.DataBind();
         }
    }

    The data item for the particular row is stored in e.Item.Data, so you'll need to access that to determine which CD it is that you're needing to get the tracks for.  Then you set the nested repeaters data item to those tracks, and data bind it so it writes out all the tracks.  Know that the ItemDataBound may fire for the header and the footer rows, which is why you want to check the item type before attempting to databind the nested repeater.

    Hope that helps.


    Damon Armstrong, Technology Consultant
    [Blog] [Articles]
  •  07-27-2007, 5:34 AM Post number 34140 in reply to post number 925

    Re: nesting repeaters in asp.net

    Hey Daemon

    I'm having the same problem with a nested repeater, but I just can't seem to get it right.

    How does your GetTrackInfo method look like?

    Hope hearing from tou.

    Greetings

    Dotdonk

  •  07-30-2007, 11:21 PM Post number 34262 in reply to post number 34140

    • Damon is not online. Last active: 11-26-2008, 11:06 AM Damon
    • Top 10 Contributor
    • Joined on 06-26-2006
    • Dallas, TX
    • Acorn Archimedes

    Re: nesting repeaters in asp.net

    The GetTrackInfo method is a "hypothetical" method that returns an enumerable object (e.g. an object containing a collection of data like a collection or dataset) containing track information. 

    In this case, I would think it would be a call to the database to select track information based on the CD (we pass the CD information into the method to give the method access to that CD information). 

    Are you currently getting a specific error?


    Damon Armstrong, Technology Consultant
    [Blog] [Articles]
  •  04-23-2008, 10:21 AM Post number 48250 in reply to post number 34262

    • docgecko is not online. Last active: 04-24-2008, 10:41 AM docgecko
    • Not Ranked
    • Joined on 04-23-2008
    • Portugal
    • Level 1: Deep thought

    Re: nesting repeaters in asp.net

    Hi Damon,

    Just is a little late after you posted this idea, but I am just working on nested repeaters and I am using Linq to obtain the data.

    So I was wondering if you could:

    1) give me an example of what the GetTrackInfo method would look like, and
    2) can you also explain what the "CDInfo" is in "GetTrackInfo((CdInfo)e.Item.DataItem)", so that I can compare it to what I am doing?

    For your information, I have a number of Discs (as in Discography) (like your CDs) and each has its list of Tracks, as you suggested.  But when I try to bind my inner repeater using the OnItemDataBound from the outer repeater, I am only able get tracks for my first Disc, which is very strange.

    If you need some more info from me, please ask.

    Anyway, I hope you can help me with this.

    Regards,


    docgecko

  •  04-23-2008, 10:46 AM Post number 48252 in reply to post number 48250

    • Damon is not online. Last active: 11-26-2008, 11:06 AM Damon
    • Top 10 Contributor
    • Joined on 06-26-2006
    • Dallas, TX
    • Acorn Archimedes

    Re: nesting repeaters in asp.net

    Let's start with what CDInfo is in the example.  When you data bind a repeater you have to pass it a collection of items.  Those items can be anything: strings, integers, objects, etc, but they tend to be collections of objects.  When you reach the ItemDataBound event in the repeater, however, you are not working the collection, you are working with a single item from that collection, and that item is located in the e.Item.DataItem (e is the EventArgs parameter, and it could be named differently).  Since e.Item.DataItem is defined as an Object, you have to cast it from an Object into whatever type of object you actually need.

    So, my assumption in the example was that people would be binding a collection of CDInfo objects to the Repeater.  The code

    ((CdInfo)e.Item.DataItem

    Is simply casting the current data item into a CdInfo object.  And the code

    GetTrackInfo((CdInfo)e.Item.DataItem);

    Is my way of saying, hey, you need to get the track information for the CD.  The method itself should return a collection (an array, IEnumerable, IEnumerable<>, etc) object containing the track information for the current CD.  And then you bind that collection to the inner repeater and you should have a hiearchy that looks like

    CD1
         TRACK 1
         TRACK 2
         ...
    CD 2
         TRACK 1
         TRACK 2
         ...

    LINQ is good at building out object hiearchies so you probably don't need an actualy method to find the track info, it's probably already there.  So you're code may simply look like

    NestedRepeater.DataSource = ((WhateverYourLinqObjectIs)e.Item.DataItem).Tracks;

    And if you are getting the same tracks for each CD, you may want to check on the objects before the repeater is even involved to make sure the LINQ objects are being populated correctly.  If not, then you're problem is further upstream.  If so, then it's something directly in the repeater binding code.

     


    Damon Armstrong, Technology Consultant
    [Blog] [Articles]
  •  05-26-2008, 4:43 AM Post number 55675 in reply to post number 907

    Re: nesting repeaters in asp.net

    i want too so please any one has anser please send me at mitesh@polluxsoftech.com

    ________________________________
    Offshore Outsourcing
    SEO Services 

View as RSS news feed in XML