无法在GridView中显示图像

时间:2016-08-23 01:30:00

标签: c# asp.net webforms

我无法在gridview中显示数据库中的图像。我能够保存它们。当我尝试检索它时,不显示任何东西,只是产品名称。

这是我的GridView。

<Columns>
        <asp:CommandField ShowDeleteButton="True" />
        <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
         <asp:ImageField DataImageUrlField = "ProductImagesID"
            DataImageUrlFormatString = "../ImageHandler.ashx?ProductImagesID={0}"
            ControlStyle-Width = "75" ControlStyle-Height = "75"
            HeaderText = "Preview Image">
                <ControlStyle Height="100px" Width="100px"></ControlStyle>
            </asp:ImageField>
    </Columns>

图像处理程序:

public void ProcessRequest(HttpContext context)
    {
        Byte[] productImage;
        if (context.Request.QueryString["ProductImageId"] != null)
        {
            int productImageId = Convert.ToInt32(context.Request.QueryString["ProductImageId"]);

            productImage = ProductImageBL.GetImage(productImageId);

            if (productImage != null)
            {
                context.Response.BinaryWrite(productImage);
                context.Response.End();
            }
        }
    }

ProductImageBL GetImage方法:

public static Byte[] GetImage(int productImageId)
    {
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select ProductImage from ProductImages where ProductImageId = @ProductImageId";

        cmd.Parameters.AddWithValue("@ProductImageId", SqlDbType.Int).Value = productImageId;

        SqlDataReader dataReader = DbUtility.GetDataReader(cmd);
        if (dataReader.HasRows)
        {
            dataReader.Read();
            return (Byte[])dataReader[0];
        }
        else
        {
            return null;
        }
    }

请帮助我使用此代码。

先谢谢。

1 个答案:

答案 0 :(得分:0)

创建一个图像用途的页面,如GetMeImage.aspx所示,功能如下

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["ProductImagesID"] != null)
    {
string query = "Select ProductImage from ProductImages where ProductImageId = @ProductImageId";
SqlCommand cmd = new SqlCommand(query,YourConnectionObject);

    cmd.Parameters.AddWithValue("@ProductImageId", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["ProductImagesID"]);

    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();

    if (dt != null)
    {
        Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = dt.Rows[0]["ContentType"].ToString();
        Response.AddHeader("content-disposition", "attachment;filename="
        + dt.Rows[0]["Name"].ToString());
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }
}
}

在gridview中添加模板字段

<asp:ImageField DataImageUrlField = "ProductImagesID"
            DataImageUrlFormatString = "../GetMeImage.aspx?ProductImagesID={0}"
            ControlStyle-Width = "75" ControlStyle-Height = "75"
            HeaderText = "Preview Image">
                <ControlStyle Height="100px" Width="100px"></ControlStyle>
            </asp:ImageField>