问题描述:

I have an issue in my code. I am trying to add an image inside of GridView using C#, Asp.net technologies.I am retrieving image name only from the database and want to add this with the stored folder path.

faq.aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">

<Columns>

<asp:TemplateField>

<ItemTemplate>

<table class="table table-striped table-bordered margin-top-zero" >

<colgroup>

<col class="col-md-1 col-sm-1">

<col class="col-md-4 col-sm-4">

<col class="col-md-4 col-sm-4">

<col class="col-md-2 col-sm-2">

<col class="col-md-1 col-sm-1">

</colgroup>

<thead>

<tr>

<th>Sl. No</th>

<th>Question</th>

<th>Answer</th>

<th>Image</th>

<th>Action</th>

</tr>

</thead>

<tbody>

<tr>

<td><asp:Label ID="faqid" runat="server" Text='<%#Eval("FAQ_ID") %>'></asp:Label></td>

<td><asp:Label ID="question" runat="server" Text='<%#Eval("Question") %>'></asp:Label></td>

<td><asp:Label ID="answer" runat="server" Text='<%#Eval("Answer") %>'></asp:Label></td>

<td><asp:Image ID="Image1" runat="server" border="0" name="bannerimage" style="width:70px; height:70px;" ImageUrl='C:\ASP.NET\ODIYA_Doctor_Admin\ODIYA_Doctor_Admin\Upload\<%#Eval("Image")%>' /> </td>

<td><a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="Edit"><i class="fa fa-edit"></i></a>

<a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-danger" data-original-title="Delete"><i class="fa fa-times"></i></td>

</tr>

</tbody>

</table>

</ItemTemplate>

</asp:TemplateField>

</Columns>

faq.aspx.cs:

protected void Page_Load(object sender, EventArgs e)

{

faqBL objFaqBL = new faqBL();

GridView1.DataSource = objFaqBL.getFaqData();

GridView1.DataBind();

}

I am getting all the other data except image in the view. Image is also field name in my table. Please help me to resolve this issue.

网友答案:

Assuming that your're storing the image in SQL Server database, you need to create Generic handler image.ashx or image.aspx as below: 1. Create image.aspx page add this code to the Page_Load :

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["id"] != null)
    {
        string sql1 = "SELECT Image FROM" +
            " YourTableName WHERE faqid=" +  Convert.ToInt32(Request.QueryString["id"].ToString());

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "your connection string";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            if (dr[0] != DBNull.Value)
            {
                Byte[] bytes = (Byte[])dr[0];
            }
            else
            {
                byte[] bytes = ReadFile(Server.MapPath("~/images/empty.PNG"));
                Response.Buffer = true;
            }
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

        dr.Close();
        conn.Close();


    }
}

byte[] ReadFile(string sPath)
{
    byte[] data = null;
    FileInfo fInfo = new FileInfo(sPath);
    long numBytes = fInfo.Length;
    FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fStream);
    data = br.ReadBytes((int)numBytes);
    return data;
}
  1. To view the image in the GridView, add a template column as below :

           <asp:TemplateField HeaderText="Image">
                <ItemTemplate>
                    <div style="text-align:center;">
                    <asp:ImageButton ID="ImageButton1" runat="server" 
                    ImageUrl='<%# "image.aspx?id="+ Eval("faqid") %>' Width="100px"
                    Height="70px" Style="cursor: pointer"  />
                    </div>
                </ItemTemplate>
                <ItemStyle Width="300px" HorizontalAlign="Left" />
            </asp:TemplateField>
    

Hope that helps.

网友答案:

As Ondrej said, ImageUrl works with with URL like example.com/Upload/myimage.png. Assuming your Upload folder is in the root of your web application it would work if you change the ImageUrl to

ImageUrl='<%# "Upload/" + Eval("Image")%>'

Give it a try :)

相关阅读:
Top