Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bind image (dynamic img src)

Status
Not open for further replies.

hamking01

Programmer
May 30, 2004
238
US
I'm trying to bind an image to a form. Where the name of the image is built dynamically. For example, if the querystring is 1, DB will pull record for 1 to find imagename field.

trying to bind to <img src="<%=imagename%>.jpg/>

how would i go about doing this. The image is not placed in a repeater or datagrid, neither does it have a "id" such as that of textboxes and labels. So I can't have the sub reference the img src.
 
Use an Image control.
Code:
<asp:Image runat="server" id="dynamicImage" />

In your code, you can refer to it and set it's properties as you like:

Code:
Me.dynamicImage.ImageUrl = "../images/xyz.gif"
Me.dynamicImage.Visible = True
me.dynamicImage.Width = 
'etc....

Greetings,
Dragonwell
 
What i'm trying to do is to have the page load the appropriate image based on the querystring. For example, if the the querystring has id=1 it'll load the image 1.jpg,
if id=2 will load 2.jpg.
 
Sure. In your PageLoad method, just get the value from the QueryString and use it to set the url of your Image

[VB]
Code:
Dim imageID as String
If Not Request.QueryString("id") is Nothing Then
   
    imageID = Request.QueryString("id")
    
    Me.dynamicImage.ImageUrl = "images/" + imageID + ".jpg"

End If

Greetings,
Dragonwell
 
what if i need to load images that aren't saved as the querystring id names. For example if id=1 it'll connect to DB and find the imagename (ie. imagename1) field for that record and output accordingly.

With textboxes, labels, datagrids, repeater i can connect to database and bind with SOMETHING.databind or SOMETHING.text, in which SOMETHING is the id name of either of the four. I can't figure how to do the same to load a image.
 
Figured it out as follows:

Sub PicBind()
Dim sqldisplay as String
sqldisplay = "SELECT * FROM QL WHERE " & GetDetail
Dim conn as OleDbConnection
conn = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\local\acs\Q.mdb")
conn.Open()
Dim cmd as OleDbCommand
cmd = new OleDbCommand (sqldisplay, conn)
Dim reader as OleDbDataReader
reader = cmd.ExecuteReader()
if (reader.Read()) Then
dyImage.ImageUrl = "pic/" & reader("CFItemNO").ToString() & "L.jpg"
End if
conn.Close()
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top