Loop through controls???

  • Thread starter Thread starter Jay
  • Start date Start date
J

Jay

If I have 10 imagebuttons on a form (imagebutton1, imagebutton2,
imagebutton3, etc.) in asp.net and I want to loop through each one and set
their imageurl in a loop how can this be done.

Something like :

dim i as int16
for i = 1 to 10
imagebutton(i).imageurl="whatever.jpg"
next

The code above does not work... is it possible to do this?

Thanks.
 
dim ctl as Control
for each ctl in me.controls
if typeof ctl is imageButton
ctype(ctl, imageButton).imageurl = "whatever.jpg"
end if
next
 
I'm not sure if they applies to asp.net, but when using
WindowsForms you can use the control's Controls
collection. e.g.:

For i As Integer = 0 to Me.Controls.Count - 1
If (TypeOf Me.Controls.Items(i) Is ImageButton) Then
...
End If
Next i

Hope that helps.
Lance
 
Jay said:
If I have 10 imagebuttons on a form (imagebutton1, imagebutton2,
imagebutton3, etc.) in asp.net and I want to loop through each one and set
their imageurl in a loop how can this be done.

Something like :

dim i as int16
for i = 1 to 10
imagebutton(i).imageurl="whatever.jpg"
next

Error message?!
 
Srinivas Kotipalli said:
dim ctl as Control
for each ctl in me.controls
if typeof ctl is imageButton
ctype(ctl, imageButton).imageurl = "whatever.jpg"

You may want to use 'DirectCast' instead of 'CType'.
 
Thanks a lot. But what if I only wanted to set the imageurl for
imagebutton1, 2, 3 - 10? If there were other imagebuttons on the page they
would be set as well.
 
use a consistent naming convention for the image controls (e.g. image1,
image2 etc)
then something like
dim x as int16
dim ctrlname as string
dim img as Control
for x=1 to 10 ' or whatever
ctrlname="image" & x
img=FindControl(ctrlname) ' find name control
img.imageurl="someimage.jpg"
next

Hope that helps

Roger
 
'This worked for me on a series of textboxes named
'txtPartnumber1 through txtPartnumber10 :

Dim ctlname As String
Dim ctltextbox As Control

For x = 1 To 10
ctlname = "txtPartnumber" & x
ctltextbox = FindControl(ctlname)
CType(ctltextbox, TextBox).Text = "1234567"
Next
 
Back
Top