Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > how to get images from code-behind?

Reply
Thread Tools

how to get images from code-behind?

 
 
Bob
Guest
Posts: n/a
 
      03-30-2009
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


 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      03-30-2009
On Mar 30, 11:06*am, "Bob" <(E-Mail Removed)> wrote:
> 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") %>'
 
Reply With Quote
 
 
 
 
Andrew Morton
Guest
Posts: n/a
 
      03-30-2009
Bob wrote:
> 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


 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      03-30-2009
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" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
On Mar 30, 11:06 am, "Bob" <(E-Mail Removed)> wrote:
> 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") %>'


 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      03-30-2009
On Mar 30, 11:32*am, "Bob" <(E-Mail Removed)> wrote:
> 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" <(E-Mail Removed)> schreef in berichtnews:(E-Mail Removed)...
> On Mar 30, 11:06 am, "Bob" <(E-Mail Removed)> wrote:
>
>
>
> > 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") %>'


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
 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      03-30-2009
Thanks

"Alexey Smirnov" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
On Mar 30, 11:32 am, "Bob" <(E-Mail Removed)> wrote:
> 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" <(E-Mail Removed)> schreef in
> berichtnews:(E-Mail Removed)...
> On Mar 30, 11:06 am, "Bob" <(E-Mail Removed)> wrote:
>
>
>
> > 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") %>'


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
DataList with RadioButtons + Images: How to click on the images to activate the radiobutton? Luis Esteban Valencia Muņoz ASP .Net 0 06-03-2005 03:22 PM
Track Images added to images folder and subfolders - send out email alerts ASP .Net 4 01-04-2005 10:01 PM
Can recover old images from SmartMedia card, but not recent images Stephen Stormont Digital Photography 9 10-20-2003 07:59 PM



Advertisments