1

I want to display image within template field of the gridview. I want to know how can I pass Image url for the Image control. This is what I am doing- ASPX Page:

<asp:GridView ID="GridView1" runat="server" Width="1000px" CellPadding="4" 
                    ForeColor="#333333" GridLines="None" AutoGenerateColumns="False">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Image ID="Image1" runat="server" Height="122px" 
                                    ImageUrl="" Width="148px" />
                                <br />
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="300px" />
                        </asp:TemplateField></asp:GridView>

.CS Code:

public void show()
{
    try
    {
        dt = g1.return_dt("select * from tbl_item_post where Ref_No is not null order by company");
        if (dt.Rows.Count > 0)
        {
            pic1 = Convert.ToString(dt.Rows[0]["image_url"]);
            adsource = new PagedDataSource();
            adsource.DataSource = dt.DefaultView;
            adsource.PageSize = 10;
            GridView1.DataSource = adsource;
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
}

Please Guide me how to do this.

1
  • didn't any of the answers help ?? Commented Dec 23, 2013 at 10:46

3 Answers 3

4

You can directly bind the image url in the gridview like this

<asp:Image ID="Image1" runat="server" Height="122px" ImageUrl='<%# Eval("image_url","~/Images/{0}") %>' Width="148px" />

You can always able to create the correct url depending upon what values are you saving in the database for the image.

Sign up to request clarification or add additional context in comments.

Comments

0

You can directly use Eval for DataBinding

 <ItemTemplate>
                <asp:Image ID="Image1" runat="server" Height="122px" 
                                ImageUrl='<%#Eval("image_url")%>' Width="148px" />
                            <br />
 </ItemTemplate>

1 Comment

Since I am saving the files in a folder as well as in the database, in that case it is also necessary to provide folder name along with the eval.
0

You can concatenate string and table field using this:

<ItemTemplate> 
  <asp:Image ID="imgFlag" CssClass="imgright" runat="server" ToolTip="" ImageUrl='<%# "/a_img/a_site/ico16/flag16_"+Eval("doclngid")+ ".gif"  %>'>
</ItemTemplate>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.