Link to home
Start Free TrialLog in
Avatar of arhame
arhameFlag for United States of America

asked on

Looping through a nested Datalist

I've got a setup like this:
Datalist1
    Gridview1
    Gridview2.... etc
    Datalist3
        Gridview30
        Gridview31
        Gridview32


Gridview 30, 31, etc is surrounded by a Panel.  I'm setting the Panels.visible = false based on the criteria in the Gridview.  I have this working... sort of.

What I need help with, is it works - but it only works on the first "item" produced by the Datalist3.  If Datalist3 produces 4 Items (4 sets of 3 gridviews), the 2nd 3rd and 4th set won't listen to my code.  How do I loop through Detailsview3's items and do it for each one?

I've tried the following:

    Protected Sub DataList3_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
        Dim dl3 As DataList = CType(FindC("Datalist3", DataList1.Controls), DataList)
        Dim item As DataListItem
        For Each item In dl3.Items <---------------------ERROR HERE
            Dim gv30 As GridView = CType(FindC("GridView30", DataList1.Controls), GridView)
            If gv30.Rows.Count = 0 Then
                Dim pnl1 As Panel = CType(FindC("pnlDStrut", DataList1.Controls), Panel)
                pnl1.Visible = False
            End If
            ..... Etc
        Next item
       
    End Sub

But it error's out on the line mentioned above, saying the object reference is not set to an object.  I assume since I'm trying to access the Datalist3 in it's own ItemDataBound event, the control isn't completely rendered so it can't find it?  I don't know...

Does anybody know what I'm doing wrong and how I can get it to loop through the Datalist3's items and apply the code to each item in it rather than just the first item it produces?

Thanks a ton!
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Does that mean that dl3.Items.Count = 1?

Bob
Avatar of arhame

ASKER

Hey Bob,

Thanks for your response.

No, if in Datalist1_ItemDatabound event I put Response.write(dl3.items.count.tostring()) it returns a value of 20.
Well, does it loop through the For loop 20 times?

Bob
Avatar of arhame

ASKER

No, it doesn't.  That's actually where I'm stuck, getting the loop to work.

I get an error when I try to loop through for each item in the datalist.

Datalist3, is a nested control inside Datalist1.

Under Datalist3_ItemDatabound I have:

Dim dl3 As DataList = CType(FindC("Datalist3", DataList1.Controls), DataList)
        Dim item As DataListItem
        For Each item In dl3.Items <---------------------ERROR HERE

Object Reference not set to an instance of an object.  I'm assuming since I have it trying to loop through Datalist3, on datalist3_itemdatabound event.  

I had _assumed_ since it was in the item_databound event of the datalist it'd automatically repeat itself for each item... however, it only works on the first "set" of gridviews, the other 19 don't get touched.

So I'm doing something wrong, I was just assuming I needed to add a for each loop in the item_databound... I could be going in the complete wrong direction.
Usually, if you get an "Object reference not set" exception, it is because it can't find the control 'dl3'.  

Bob
Avatar of arhame

ASKER

Right.  I think it's because I'm trying to find the control, in it's own _itemdatabound event... If I put it in Datalist1_ItemDatabound it finds it fine.  It just can't find itself when I put it in it's _itemdatabound - that's where I think my logic is failing and looking for another way to do it.

The way I know to loop through a datalist's controls, is to refer to it by name:
Button1_click:
Dim item as DatalistItem
For each item in Datalist1.Items

Next item

However, with working on a nested Datalist you can't just refer to the item, you obviously have to cast it first.  And the cast won't work in it's own _itemdatabound event.

I'm assuming that's it, since if I put the same code to find the item in Datalist1_ItemDatabound it finds it fine and can tell me how many rows are in it and such.  But if I put the code to hide the panels in the Datalist1_ItemDatabound event - the changes don't take place.
Avatar of arhame

ASKER

I'm thinking instead of calling the datalist3 directly, since it's already in the datalist3_itemdatabound event I need to do something like:

For Each item In e.Item.Controls

But that error's out with:

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.DataListItem'.

So I'm just not quite sure how to reference the datalist, inside of it's own _itemdatabound event.
Avatar of arhame

ASKER

The more I think about it, the more I really think I'm going about this the wrong way - but not sure how to fix the issue.  I dont' think I should have to loop through each item, when the code is placed in the _itemDatabound event.  It should run each time for each item anyway.  But it's not.

So for clarification I have:

Datalist1
    Gridview1
    Datalist3
        GridviewA
        GridviewB
        GridviewC


Lets say Datalist3 returns 3 items.  So I have:
Item1
    GridviewA
    GridviewB
    GridviewC

Item2
    GridviewA
    GridviewB
    GridviewC

Item3
    GridviewA
    GridviewB
    GridviewC

I have code that will check the value of the gridview, and make it visible or not based on if anything is in the gridview.  When I run it though, it will hide gridviewa,b,c in item1 appropriately - however in item's 2 and 3 it doesn't hide them even though they meet the criteria.

So for some reason, it's only working on the first item, not following ones.  So I guess I need to know what I might be doing wrong... wrong event?
I don't understand what you are trying to ultimately achieve.  

Are you just trying to get this?

            Dim gv30 As GridView = CType(FindC("GridView30", DataList1.Controls), GridView)
            If gv30.Rows.Count = 0 Then
                Dim pnl1 As Panel = CType(FindC("pnlDStrut", DataList1.Controls), Panel)
                pnl1.Visible = False
            End If


Bob
Avatar of arhame

ASKER

I'm sorry for not being clear enough.  I'll try to explain it thoroughly this time.  As mentioned above, I've got a nested Datalist, inside another Datalist.  The nested Datalist contains 3 gridviews.  Those three Gridviews are surrounded by a Panel each.  Based on the values in the Gridviews, I want to make the Panel visible or not.  So the setup looks like this:

So I have:
Datalist1
    Datalist3
    <panela>GridviewA</panela>
    <panelb>GridviewB</panelb>
    <panelc>GridviewC</panelc>

So to achieve this, I use the code you mentioned (and was in my first post) in Datalist3_ItemDatabound event:

       Dim gv30 As GridView = CType(FindC("GridView30", DataList1.Controls), GridView)
        If gv30.Rows.Count = 0 Then
            Dim pnl1 As Panel = CType(FindC("pnlDStrut", DataList1.Controls), Panel)
            pnl1.Visible = False
        End If


So, that is my setup.  Now, this is my issue:

Say Datalist3 has more than one item to show.  So it presents 3-4 sets of the 3 Gridviews inside of it.  The code I'm using to make Panel invisible works, but only on the first set.  So for example if Datalist3 presented from the Database the following datasets.

Datalist3_Item1
    GridviewA
    GridviewB
    GridviewC

Datalist3_Item2
    GridviewA
    GridviewB
    GridviewC

Datalist3_Item3
    GridviewA
    GridviewB
    GridviewC


Datalist3_Item1 in the following example would use the code, and it would set the panels to not visible.  However Datalist3_Items2 and Datalist3_Item3's Gridview's would still be visable, like it didn't run the code for them - even though the Gridviews meet the criteria to fire off the code.  It only works on the first "item" returned by the Datalist, so the first 3 Gridviews.
ASKER CERTIFIED SOLUTION
Avatar of arhame
arhame
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arhame

ASKER

Thanks for attempting to understand what the issue was Bob, it was difficult to explain and I apologize again for not doing it clearly enough the first time.