2013-02-09 42 views
0

我有一个表BIKETYPE {BIKETYPEID,NAME,DESCRIPTION,IMAGE}。从数据库中检索图像并将它们注入到列表视图中

形象的image数据类型。我试图通过列表视图来显示表格。我能够看到除图像栏外的所有内容。

我的代码遵循

<ItemTemplate> 
    <tr> 
     <td><%# DataBinder.Eval(Container.DataItem,"BikeTypeId") %></td> 
     <td><%# DataBinder.Eval(Container.DataItem,"Name") %></td> 
     <td><%# DataBinder.Eval(Container.DataItem,"Description") %></td> 
     <td><asp:Image ImageUrl='<%# "Handler.ashx?BikeTypeId="+ Eval("image") %>' ID="Image" runat="server" /></td> 
     <td><asp:Button ID="Select" runat="server" Text="Select" CommandName="Select" /></td> 
    </tr> 
</ItemTemplate> 

在后面的代码,我使用的简单绑定方法如下

protected void bind() 
{ 
     adp = new SqlDataAdapter("Select * From BikeType", str); 
     ds = new DataSet(); 
     adp.Fill(ds); 
     ListView1.DataSource = ds; 
     ListView1.DataBind(); 
     ds.Clear(); 
     adp.Dispose(); 
} 

有什么建议?

+0

看看这篇文章(http://imar.spaanjaars.com/414/storing-uploaded-files-in-a-database-or-in-the-file-system-with -aspnet-20)和[this](http://odetocode.com/articles/172.aspx)..它会用一个示例 – 2013-02-09 04:55:36

回答

1

通过使用Genric Handler,可以将图像显示到列表视图或任何其他控件中。 添加Genric处理器通过添加新的项目> Genric处理程序,然后请参阅下面的代码

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 
namespace letsride 
{ 
    /// <summary> 
    /// Summary description for Handler1 
    /// </summary> 
    public class Handler1 : IHttpHandler 
    { 

     public void ProcessRequest(HttpContext context) 
     { 
      int id = int.Parse(context.Request.QueryString["b_id"]); 
      string constr = ConfigurationManager.ConnectionStrings["bikewebConnectionString"].ConnectionString; 
      SqlConnection con = new SqlConnection(constr); 
      con.Open(); 
      SqlCommand cmd = new SqlCommand(); 
      cmd.Connection = con; 
      cmd.CommandText = "Select image from Biketype where [email protected]"; 
      cmd.Parameters.AddWithValue("id", id); 

      object img = cmd.ExecuteScalar(); 

      try 
      { 
       context.Response.BinaryWrite((byte[])img); 
      } 
      catch (Exception ex) 
      { 
       context.Response.Write(ex.Message); 



      } 
     } 

     public bool IsReusable 
     { 
      get 
      { 
       return false; 
      } 
     } 
    } 
} 
在Web窗体

使用图片作为

<asp:Image ID="i" runat="server" ImageUrl='<%# "Handler.ashx?b_id=" + Eval("BikeTypeId") %> ' /></td> 

BikeTypeID是表的数据库 也ID参考http://makhaai.blogspot.com.au/2010/11/image-handling-in-aspnet-part-1.html

+0

+1来解释,我刚刚从中学到了两件事:如何使用处理程序以及如何从数据库检索图像并使用它。谢谢 – n3verLieMe 2015-04-29 05:36:26

相关问题