2010-06-17 56 views
1

我想從Northwind數據庫(類別表,存儲在數據庫中的圖像)加載到網格視圖控件中的圖像。但它dosenot似乎工作。 PLZ!看看...無法使用模板在gridview中顯示圖像

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 [email protected]"; 
        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); 
      } 
     } 
    } 

回答

0

您需要設置內容類型的通用處理器

context.Response.ContentType = "image/bmp"; 

嗯..打開處理程序與查詢字符串輸入,直接從您的瀏覽器...這是否工作?

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

你應該會看到一個錯誤信息,如果它失敗。

+0

nop!它仍然沒有工作:( – Neel 2010-06-17 21:36:24

+0

你確定它是一個位圖圖像,你存儲在數據庫中嗎? – IrishChieftain 2010-06-17 22:17:53

+0

http://abc/yourproject/ImageHandler.ashx?CategoryID = 2,破碎的圖像圖標:( – Neel 2010-06-18 04:20:46