how to get images from code-behind?

  • Thread starter Thread starter Bob
  • Start date Start date
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
 
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") %>'
 
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
 
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


"Alexey Smirnov" <[email protected]> schreef in bericht
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") %>'
 
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

"Alexey Smirnov" <[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
 
Thanks

"Alexey Smirnov" <[email protected]> schreef in bericht
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

"Alexey Smirnov" <[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
 
Back
Top