C# 无法在GridView中显示图像
我无法在gridview中显示数据库中的图像。我能救他们。当我试图检索时,它没有显示任何东西,只显示一个产品名称 这是我的GridViewC# 无法在GridView中显示图像,c#,asp.net,webforms,C#,Asp.net,Webforms,我无法在gridview中显示数据库中的图像。我能救他们。当我试图检索时,它没有显示任何东西,只显示一个产品名称 这是我的GridView <Columns> <asp:CommandField ShowDeleteButton="True" /> <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName"
<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>
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;
}
}
请帮我翻译一下这个代码
提前感谢。创建一个用于图像目的的页面,例如
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>
请编辑您的问题以包含代码的相关部分。这个社区不喜欢代码的外部链接,如果您直接提供代码,您的问题很可能会得到答案。
<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>