简体   繁体   English

从数据库中检索图像并将其注入到列表视图中

[英]Retrieving images from the database and injecting them into listview

I have a table BIKETYPE { BIKETYPEID, NAME, DESCRIPTION, IMAGE}. 我有一个表BIKETYPE {BIKETYPEID,NAME,DESCRIPTION,IMAGE}。

IMAGE has a datatype of image . 形象的数据类型image I am trying to display the table through list view. 我正在尝试通过列表视图显示表格。 I am able to see everything except the image column. 我可以看到除图像列以外的所有内容。

My code is as follow 我的代码如下

<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>

In the code behind, I am using simple bind method as follow 在后面的代码中,我正在使用简单的bind方法,如下所示

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();
}

Any suggestions? 有什么建议么?

the images can be displayed into listview or any other control by using Genric Handler. 可以使用通用处理程序将图像显示到列表视图或任何其他控件中。 Add A Genric Handler By Adding new Item > Genric Handlers and then Refer to the Code Below 通过添加新项目>通用处理程序来添加通用处理程序,然后参考下面的代码

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 BikeTypeId=@id";
            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;
            }
        }
    }
}

in your Web Forms use image as 在您的Web窗体中使用image作为

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

BikeTypeID is the ID of table in database also refer to http://makhaai.blogspot.com.au/2010/11/image-handling-in-aspnet-part-1.html BikeTypeID是数据库中表的ID,也请参考http://makhaai.blogspot.com.au/2010/11/image-handling-in-aspnet-part-1.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM