[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Looping through a nested Datalist

Posted on 2008-02-06
13
Medium Priority
?
1,833 Views
Last Modified: 2008-02-29
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!
0
Comment
Question by:arhame
  • 8
  • 4
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20836248
Does that mean that dl3.Items.Count = 1?

Bob
0
 
LVL 8

Author Comment

by:arhame
ID: 20836500
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20836535
Well, does it loop through the For loop 20 times?

Bob
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 8

Author Comment

by:arhame
ID: 20836573
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20836594
Usually, if you get an "Object reference not set" exception, it is because it can't find the control 'dl3'.  

Bob
0
 
LVL 8

Author Comment

by:arhame
ID: 20836688
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
 
LVL 8

Author Comment

by:arhame
ID: 20836726
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
 
LVL 8

Author Comment

by:arhame
ID: 20836815
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20840829
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
 
LVL 8

Author Comment

by:arhame
ID: 20841069
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
 
LVL 8

Accepted Solution

by:
arhame earned 0 total points
ID: 20841565
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
 
LVL 8

Author Comment

by:arhame
ID: 20841615
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

612 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