473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Loop through all items in an aspx page.

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(Objec t sender, EventArgs e)
in C:\Development\glimpse\ActionItems\ActionItems\Ite mSecurity.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
Nov 18 '05 #1
4 3696
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.

"Ryan Ternier" <rt******@icompasstech.com.spamproof> wrote in message
news:O2**************@TK2MSFTNGP11.phx.gbl...
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(Objec t sender, EventArgs e) in C:\Development\glimpse\ActionItems\ActionItems\Ite mSecurity.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

Nov 18 '05 #2
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

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:ON**************@TK2MSFTNGP11.phx.gbl...
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.

"Ryan Ternier" <rt******@icompasstech.com.spamproof> wrote in message
news:O2**************@TK2MSFTNGP11.phx.gbl...
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(Objec t sender, EventArgs

e)
in

C:\Development\glimpse\ActionItems\ActionItems\Ite mSecurity.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


Nov 18 '05 #3
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

"Ryan Ternier" wrote:
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(Objec t sender, EventArgs e)
in C:\Development\glimpse\ActionItems\ActionItems\Ite mSecurity.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

Nov 18 '05 #4
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" <Jo*********@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
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

"Ryan Ternier" wrote:
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(Objec t sender, EventArgs e) in C:\Development\glimpse\ActionItems\ActionItems\Ite mSecurity.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

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: jason | last post by:
Access 2000: I have a customer-inventory table I need to loop through and compile a list of all the inventory items the customer is tracking. The problem I am finding is that a simple loop...
11
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
2
by: JP | last post by:
Hi, I am trying to loop through the listbox and read the selected items from the list, within a CLICK event on an aspx page. The following is what I have tried. It loops through the listbox,...
1
by: Tim | last post by:
Hi, I'm very new to .NET and am programming in C#. I have a web application where i have two list boxes. Its kind of like a shopping card where you can add items from one 'locations' list box to...
5
by: A.M | last post by:
Hi, My ASP.NET application uses SSL on IIS6. up on visiting some pages, IE 6 shows this security alert: This page contains both secure and non secure items. Do you want to display non-secure...
11
by: Alexander Bosch | last post by:
Hi, I'm having a problem similar to the one that's stated in this KB http://support.microsoft.com/default.aspx?scid=kb;en-us;839521 When I'm posting a page to itself with the bool value as true it...
16
by: Islam Elkhayat | last post by:
In my web application i use satalite assembly to localize my web form.. Instead of set the each control text to the resourcemanager getstring() method i try to use foreach loop private string...
1
by: Dave | last post by:
I have a button in the same page as the repeter control, so when I click that I need to loop threw the repeater and get the value of each textbox and id Dave
6
by: TCook | last post by:
Hello, I was wondering if anyone has a code snippet for looping through a 'select' control's 'option' elements? Do I have to use an ASP.Net web control such as an asp list control or dropdown...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.