how to get images from code-behind?

Discussion in 'ASP .Net' started by Bob, Mar 30, 2009.

  1. Bob

    Bob 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
     
    Bob, Mar 30, 2009
    #1
    1. Advertising

  2. On Mar 30, 11:06 am, "Bob" <> 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") %>'
     
    Alexey Smirnov, Mar 30, 2009
    #2
    1. Advertising

  3. 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
     
    Andrew Morton, Mar 30, 2009
    #3
  4. Bob

    Bob 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


    "Alexey Smirnov" <> schreef in bericht
    news:...
    On Mar 30, 11:06 am, "Bob" <> 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") %>'
     
    Bob, Mar 30, 2009
    #4
  5. On Mar 30, 11:32 am, "Bob" <> 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" <> schreef in berichtnews:...
    > On Mar 30, 11:06 am, "Bob" <> 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
     
    Alexey Smirnov, Mar 30, 2009
    #5
  6. Bob

    Bob Guest

    Thanks

    "Alexey Smirnov" <> schreef in bericht
    news:...
    On Mar 30, 11:32 am, "Bob" <> 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" <> schreef in
    > berichtnews:...
    > On Mar 30, 11:06 am, "Bob" <> 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
     
    Bob, Mar 30, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    654
    Alphonse Giambrone
    Jun 28, 2003
  2. aaa
    Replies:
    3
    Views:
    4,822
    Verde
    May 6, 2005
  3. Daniel Manes

    Code-Behind Pain in the Behind!

    Daniel Manes, Jun 10, 2005, in forum: ASP .Net
    Replies:
    11
    Views:
    713
  4. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    537
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  5. Mike
    Replies:
    1
    Views:
    323
    Alessandro Zifiglio
    Jan 23, 2004
Loading...

Share This Page