Search code examples
asp.netvb.netimageurl

Accessing images programmatically in asp.net


I am using visual studio 2008 coding asp.net.vb

I have 20 images on my site, the image holders being named picbox1 picbox2 picbox3 ... picbox20.

I want to be able to address each picbox programmatically; pseudo code would look something like this

if myvar = 1 then
picbox(myvar).imageurl="XXXXXXX"
end if

Can this be done and if so how?

Ah sorry should have said, I need to do this server side as part of my vb code.

Thank you for all and any help.


Solution

  • You can loop through the control in your form like so:

        Dim count As Integer = 1
        For Each Control In form1.Controls
            If TypeOf Control Is Image Then
                Dim img As Image = CType(Control, Image)
                If img IsNot Nothing And img.ID = "picbox" & count.ToString() Then
                    count = count + 1
                    'Do something with picbox
                End If
            End If
        Next
    

    Or you could just do a FindControl like so:

    Dim img1 As Image = CType(form1.FindControl("picbox" & myvar.ToString()), Image)

    img1.ImageUrl = "XXXX"