0

I have 5 image control in a page with Id's image1 trough image5 and I want to change those controls images in codebehind with a loop.in fact I catch some images name in a folder and after that I want assign imageUrl with this names. How can I do that ? There is a way in a loop I can access specific image control with postfix i where i is a loop counter? Or other way?

BAHA2R
  • 5
  • 5
  • About half-way down this question is an answer showing how to find a control by Id (the one with 36 upvotes) - https://stackoverflow.com/questions/4955769/better-way-to-find-control-in-asp-net also see this : https://msdn.microsoft.com/en-us/library/y81z8326(v=vs.110).aspx – PaulF Jul 19 '17 at 10:18

2 Answers2

0

You can add the 5 image controls in a List and use a foreach-loop to iterate through it

Xzibitee
  • 149
  • 4
0
For i as Integer = 1 to 5
    Dim img As Image = CType(Form.FindControl("image" & i.ToString()), Image)
    img.ImageUrl = getImageUrlForI(i)
Next

This works for images, but generally speaking you could loop through every control in the page, then check their type, and 'do your thing' depending on its type

D Ie
  • 841
  • 7
  • 23