Loop through all items in an aspx page.

R

Ryan Ternier

Thanks for the previous help guys!

I got my list box issue working, but now i'm trying to loop through all the
items in my page.

I want to find each listbox, once I do i strip the ID down to find what I
need so I can populate a DB.

lstTest is dim'd as a ListBox, and I thought I could just do:

For Each lstTest in Me.Controls to find each listbox, but I keep getting the
error:

System.InvalidCastException: Specified cast is not valid.
at ActionItems2._0.ItemSecurity.btnSubmit_Click(Object sender, EventArgs e)
in C:\Development\glimpse\ActionItems\ActionItems\ItemSecurity.aspx.vb:line
222

What's the easiest way of accomplishing this. I want to do most of it in
..NET to get my knowledge up.
I could accomplish it in JScript, but that's not helping my .NET ;)

Thanks!


Try
For Each lstTest In Me.Controls
astrListID = (lstTest.ClientID.ToString.Split("_"))
'If the string = lstY, the listbox where people are allowed to edit,
then continue.
If Left(astrListID(2), 4) = "lstY" Then
strID = astrListID(2).Remove(0, 4) 'strID is now the UserID
lstRowItem = New ListItem
For Each lstRowItem In lstTest.Items
'Grab the Security ID from the DB where this user resides.
strSQL = "Select SecurityID from tblItemSecurity where
UserID = " & strID & " AND type = 'item' AND TypeID = " &
intTypeID
cmdSecurity.CommandText = strSQL
intSecurityID = cmdSecurity.ExecuteScalar
'Now with that Security ID, let's start populate the item
field DB based on that security idish
strSQL = "INSERT into tblItemFieldSecurity (SecurityID,
FieldID) values(" & intSecurityID & ", " & lstRowItem.Value
& ")"
cmdSecurity.CommandText = strSQL
cmdSecurity.ExecuteNonQuery()
Next
End If
Next
Catch ex As Exception
 
K

Kevin Spencer

Unless every Control in the Controls Collection of your Page is a ListBox,
of course you would get an invalid cast exception. In addition, your
ListBoxes are inside the form, which is a child Control of your Page, so
there will never BE any ListBoxes in the Page's Controls Collection.

What you need to do is identify the Control that hosts your ListBoxes and
loop through that. Assuming that there are other Controls and/or HTML (which
is rendered as a LiteralControl), use Object in your For Each loop, and
test the type of the Control to see whether it is indeed a ListBox before
trying to cast it as such.

In addition, if your ListBox Controls are nested inside other Controls, in
other words not all part of the same Control's Controls Collection, you can
write a recursive function that loops through all the Child Controls of a
given Control, and calls itself for each Child Control. This results in a
recursive "tree" that loops through all the Controls UNDER a Control.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
R

Ryan Ternier

Thanks for the direction Kevin. I did some playing around between my first
post and this.
This is the structure of my document in the .aspx part:

<form>
<asp:repeater>
<ItemTemplate>
<div>
<table>

After that i have my Label, and listbox pair.

I tried this:

For Each ctrlTest In Me.Controls
If ctrlTest.GetType() Is GetType(Repeater) Then
Dim rptTest As Repeater = CType(ctrlTest, Repeater)
Dim ctrlRptTest As Control
For Each ctrlRptTest In rptTest.Controls
Dim lstTest As ListBox = CType(ctrlTest, ListBox)
astrListID = (lstTest.ClientID.ToString.Split("_"))
'If the string = lstY, the listbox where people are allowed
to edit, then continue.
If Left(astrListID(2), 4) = "lstY" Then

I missed out the form tag, i'll try that, and i'll try the object as well.
This is my first time really using a repeater, and going through an HTML
tree like this.
Thanks!
/RT
 
G

Guest

You can not use a For Each that way. The Controls collection does not only contain listboxes; this is why you are getting the InvalidCastException.

To do this, you will have to iterate through all the controls on the page, and check the type. Here is an a little snippet that does that:

Private Sub TraverseResourceTexts(ByVal container As Control)
Dim control As control

For Each control In container.Controls
DoSomething(control)
If control.HasControls() Then
TraverseResourceTexts(control)
End If
Next
End Sub

Public Sub DoSomething(ByVal control as Control)
Select Case control.GetType.Name
Case "ListBox"
'cast control to listbox here like ' CType(control, ListBox) '
End Select
End Sub

hope this helps,
John
 
R

Ryan Ternier

John, that worked perfectly!

Now just to tweak the client side javascript. It seems when I remove items
from the listbox, they don't actually
get removed because .NET is reading the listbox fully even if nothing is in
it :\

Thanks though!

/RT
John Sivilla said:
You can not use a For Each that way. The Controls collection does not only
contain listboxes; this is why you are getting the InvalidCastException.
To do this, you will have to iterate through all the controls on the page,
and check the type. Here is an a little snippet that does that:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top