无法使用模板在gridview中显示图像

时间:2010-06-17 21:31:25

标签: c# asp.net database gridview image

我正在尝试将来自northwind数据库(类别表,存储在数据库中的图像)的图像加载到网格视图控件中。但它似乎有效。 PLZ!看看......

Default.aspx的

              

    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
        AutoGenerateColumns="False" DataKeyNames="CategoryID" 
        DataSourceID="NorthWindSQLExpressConnectionString" 
        EnableModelValidation="True">
        <Columns>
            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
            <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
                SortExpression="CategoryName" />
            <asp:BoundField DataField="Description" HeaderText="Description" 
                SortExpression="Description" />
            <asp:TemplateField HeaderText="Picture" SortExpression="Picture">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Picture") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# RetriveImage(Eval("CategoryID")) %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="NorthWindSQLExpressConnectionString" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NorthwindSQLExpressConnectionString %>" 
        SelectCommand="SELECT [CategoryID], [CategoryName], [Description], [Picture] FROM [Categories]">
    </asp:SqlDataSource>

</div>
</form>

[部分] Default.aspx.cs

    protected string RetriveImage(object eval)
    {
        return ("ImageHandler.ashx?CategoryID=" + eval.ToString());
    }

[部分] ImageHandler.ashx

    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.QueryString == null)
        {
        }
        else
        {
            try
            {
                using (var sqlCon = new SqlConnection(conString))
                {
                    const string cmdString =
                        "Select picture from Categories where CategoryID=@CategoryID";
                    using (var sqlCmd = new SqlCommand(cmdString, sqlCon))
                    {
                        sqlCmd.Parameters.AddWithValue("@CategoryID", context.Request.QueryString["CategoryID"]);
                        string trmp = sqlCmd.ToString();
                        sqlCon.Open();
                        using (var sqlDr = sqlCmd.ExecuteReader())
                        {
                            sqlDr.Read();
                            context.Response.ContentType = "image/bmp"; //Added after "Ed B" Sugetion but still dosenot work :(
                            context.Response.BinaryWrite((byte[])sqlDr["Picture"]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您需要在通用处理程序

中设置内容类型
context.Response.ContentType = "image/bmp";

嗯......用你的浏览器直接从查询字符串输入打开处理程序......这有用吗?

http://yourlocalhost/yourproject/ImageHandler.ashx?CategoryID=2

如果失败,您应该看到错误消息。

答案 1 :(得分:0)

首先感谢您的回复 Ed B

似乎问题不是代码(当然除了设置ContentType =“image / bmp”) 我在这里找到了解决方案 http://www.developerfusion.com/code/5223/using-ashx-files-to-retrieve-db-images/