2014-02-06 44 views
0

我有一個圖像控件DataList控件,其ASPX代碼如何從數據庫中顯示數據列表中的圖像?

<asp:DataList ID="DataList1" runat="server" 
     RepeatDirection="Horizontal" RepeatColumns="4"> 
      <ItemTemplate> 
       <div> 
        <asp:Image ID="Image1" ImageUrl='<%#Eval("IMAGE") %>' 
        runat="server" Height="75px" Width="75px" /> 
       </div> 
      </ItemTemplate> 
     </asp:DataList> 

,我試圖從數據庫中獲取圖像的代碼是

PagedDataSource objds = new PagedDataSource(); 
     string query = "SELECT * FROM Icon"; 
     SqlConnection con = new SqlConnection(); 
     con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ICONBANKConnectionString"].ConnectionString; 
     try 
     { 
      SqlCommand cmd = new SqlCommand(query, con); 
      con.Open(); 
      //SqlDataReader dr = cmd.ExecuteReader(); 
      SqlDataAdapter da = new SqlDataAdapter(); 
      da.SelectCommand = new SqlCommand(query, con); 
      DataSet ds = new DataSet(); 
      da.Fill(ds, "images"); 
      objds.DataSource = ds.Tables[0].DefaultView; 

      DataList1.DataSource = objds; 
      DataList1.DataBind(); 
      con.Close(); 
     } 
     catch (Exception) 
     { 

      throw; 
     } 

SQL表看起來像這樣

Icon(ID, SUBCAT_ID, ALBUM_ID, SUBCAT_NAME, ALBUM_NAME, IMAGE, ICON_SIZE, 
    BLACKWHITE, NAME, DESIGNER, HITS, RATINGS, PRICE) 

當我調試代碼時,它顯示數據集中的表格行,但是當我運行該頁面時,它不顯示圖像....請指導我什麼是問題在它後面。還有一件事圖像存儲在數據庫中是二進制格式

+0

請點擊此鏈接它ACN幫助你: http://stackoverflow.com/questions/19132451/how-to-display-b inary圖像-成-A-gridview的式-ASP淨使用-C – pankeel

回答

1

您正在設置字節[]或二進制數據到ImageURL字段,這是錯誤的。圖片網址需要一個路徑。

退房這個問題的答案:

Convert from binary data to an image control in ASP.NET

在DataList控件的ItemDataBound事件,找到你的圖像控制。如果您使用的HTML標籤設置src屬性,如果你正在使用,檢查出這個答案:

//Find your image control (Image1 lets say) on the item databound event of the datalist. 
Byte[] myImage = GetMyImageFromMyDataSource(); 
String st = Server.MapPath("myImageNameOrID.jpg"); // Try Name + ID to make it unique 
FileStream fs = new FileStream(st, FileMode.Create, FileAccess.Write); 
fs.Write(myImage, 0, myImage.Length); 
fs.Close(); 
Image1.ImageUrl = "myImageNameOrID.jpg"; 

編輯:看看這個鏈接瞭解更多有關使用DataList控件的ItemDataBound事件:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemdatabound%28v=vs.110%29.aspx

相關問題