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!
LVL 8
arhameAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Does that mean that dl3.Items.Count = 1?

Bob
0
arhameAuthor Commented:
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.
0
Bob LearnedCommented:
Well, does it loop through the For loop 20 times?

Bob
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

arhameAuthor Commented:
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.
0
Bob LearnedCommented:
Usually, if you get an "Object reference not set" exception, it is because it can't find the control 'dl3'.  

Bob
0
arhameAuthor Commented:
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.
0
arhameAuthor Commented:
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.
0
arhameAuthor Commented:
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?
0
Bob LearnedCommented:
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
0
arhameAuthor Commented:
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.
0
arhameAuthor Commented:
I figured out what my issue was on my own.  It wasn't just using the code once, what it was doing was turning the first set of items invisible, multiple times.  Because I was using the FindC function to find the controls, instead of using e.item.  So by changing it from:

       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

To:

            Dim dlitem As DataListItem = e.Item
            Dim gv30 As GridView = DirectCast(dlitem.FindControl("gridview30"), GridView)
            If gv30.Rows.Count = 0 Then
                Dim pnl1 As Panel = DirectCast(dlitem.FindControl("pnlDStrut"), Panel)
                pnl1.Visible = False
            End If

It was successfully going through each item returned by the datalist, and making the proper one invisible rather than just the first one multiple times.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arhameAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.