开发者

Accessing images programmatically in asp.net

开发者 https://www.devze.com 2022-12-24 20:00 出处:网络
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 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.


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"


I'm not sure if you want to do this from server side or client side, but client side is the best, just add a javascript method in image that looks like following:

var count=0;
setTimeout(1000,getImage);
function getImage()
{

   document.getElementById('image').src='image path'+'?count='+count;
}

this will do what you want to do!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号