how to get images from code-behind?

B

Bob

Hi,

I want to get the picture into the imagebutton, but from code-behind.
I did this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Using mConnection As New SqlConnection(param.ConnectionString)
sql = "select pict from artikels where hfd='n1'"
comd = New SqlCommand(sql, mConnection)
mConnection.Open()
dtreader = comd.ExecuteReader
dtreader.Read()
'imagebutton1 does exist in aspx file
img1 = FindControl("imagebutton1")
img1.ImageUrl = dtreader.GetString(0)

But this gives only a red cross as image because the directory of the
pictures is not specified.
What i try to do is the equivalent as the Eval function like this:
<asp:ImageButton runat="server" ImageUrl='<%# Eval("pict","mypict\{0}") %>'
/>

How can i do that in code-behind?
Thanks
Bob
 
G

Guest

Hi,

I want to get the picture into the imagebutton, but from code-behind.
I did this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Using mConnection As New SqlConnection(param.ConnectionString)
 sql = "select pict from artikels where hfd='n1'"
 comd = New SqlCommand(sql, mConnection)
 mConnection.Open()
 dtreader = comd.ExecuteReader
 dtreader.Read()
'imagebutton1 does exist in aspx file
 img1 = FindControl("imagebutton1")
 img1.ImageUrl = dtreader.GetString(0)

But this gives only a red cross as image because the directory of the
pictures is not specified.
What i try to do is the equivalent as the Eval function like this:
<asp:ImageButton runat="server" ImageUrl='<%# Eval("pict","mypict\{0}") %>'
/>

How can i do that in code-behind?
Thanks
Bob

ImageUrl='<%# "mypict/" & Eval("pict") %>'
 
A

Andrew Morton

Bob said:
I want to get the picture into the imagebutton, but from code-behind.
I did this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Using mConnection As New SqlConnection(param.ConnectionString)
sql = "select pict from artikels where hfd='n1'"
comd = New SqlCommand(sql, mConnection)
mConnection.Open()
dtreader = comd.ExecuteReader
dtreader.Read()
'imagebutton1 does exist in aspx file
img1 = FindControl("imagebutton1")
img1.ImageUrl = dtreader.GetString(0)

But this gives only a red cross as image because the directory of the
pictures is not specified.
What i try to do is the equivalent as the Eval function like this:
<asp:ImageButton runat="server" ImageUrl='<%#
Eval("pict","mypict\{0}") %>' />

How can i do that in code-behind?

No need to find the control - you can refer to it directly:

imagebutton1.ImageUrl = dtreader.GetString(0)

(don't forget mConnection.Close)

Andrew
 
B

Bob

Thanks for replying ...

do you mean this:

ImageButton1.ImageUrl='<%# "mypict/" & Eval("pict") %>'

But i get a syntax error: "expression expected"
And how can i specify that i only need one particular image (those which
name is in dtreader.getstring(0))?
Thanks


Hi,

I want to get the picture into the imagebutton, but from code-behind.
I did this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles Me.Load
Using mConnection As New SqlConnection(param.ConnectionString)
sql = "select pict from artikels where hfd='n1'"
comd = New SqlCommand(sql, mConnection)
mConnection.Open()
dtreader = comd.ExecuteReader
dtreader.Read()
'imagebutton1 does exist in aspx file
img1 = FindControl("imagebutton1")
img1.ImageUrl = dtreader.GetString(0)

But this gives only a red cross as image because the directory of the
pictures is not specified.
What i try to do is the equivalent as the Eval function like this:
<asp:ImageButton runat="server" ImageUrl='<%# Eval("pict","mypict\{0}")
%>'
/>

How can i do that in code-behind?
Thanks
Bob

ImageUrl='<%# "mypict/" & Eval("pict") %>'
 
Last edited by a moderator:
G

Guest

Thanks for replying ...

do you mean this:

ImageButton1.ImageUrl='<%# "mypict/" & Eval("pict") %>'

But i get a syntax error: "expression expected"
And how can i specify that i only need one particular image (those which
name is in dtreader.getstring(0))?
Thanks

"Anon User" <[email protected]> schreef in bericht





 ImageUrl='<%# "mypict/" & Eval("pict") %>'

Hi Bob,

Sorry I was misread your post. Sure, it makes sense to do all this in
the code-behind

So when you do this
img1.ImageUrl = dtreader.GetString(0)

you may add the path as follows
img1.ImageUrl = "mypict/" & dtreader.GetString(0)

Note, that this will properly work only if your webform is located in
the parent directory, for example

root\
root\webform.aspx
root\mypict\myimage.jpg

in other case you would need to use a full path to your image, e.g. in
my example

img1.ImageUrl = "/mypict/" & dtreader.GetString(0) (Notice the slash)

If you still get the red cross, then right click on it and check what
URL is requested.

Hope this helps
 
B

Bob

Thanks

Thanks for replying ...

do you mean this:

ImageButton1.ImageUrl='<%# "mypict/" & Eval("pict") %>'

But i get a syntax error: "expression expected"
And how can i specify that i only need one particular image (those which
name is in dtreader.getstring(0))?
Thanks

"Anon User" <(e-mail address removed)> schreef in
bericht





ImageUrl='<%# "mypict/" & Eval("pict") %>'

Hi Bob,

Sorry I was misread your post. Sure, it makes sense to do all this in
the code-behind

So when you do this
img1.ImageUrl = dtreader.GetString(0)

you may add the path as follows
img1.ImageUrl = "mypict/" & dtreader.GetString(0)

Note, that this will properly work only if your webform is located in
the parent directory, for example

root\
root\webform.aspx
root\mypict\myimage.jpg

in other case you would need to use a full path to your image, e.g. in
my example

img1.ImageUrl = "/mypict/" & dtreader.GetString(0) (Notice the slash)

If you still get the red cross, then right click on it and check what
URL is requested.

Hope this helps
 
Last edited by a moderator:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top