本文介绍了在网格视图中显示保存的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我弄错了一些图像上传代码,其中图像以二进制格式成功存储在数据库中.
我的问题是,网格视图中未显示图像

这是我的数据库表图片

I have writtten some image upload code in which image is stored successfully in binary format in a database.
My problem is that image is not displayed in the gridview

This is my database table Pictures

ImgId   int

ImageName  varchar(500)
Image          image


Gridview.aspx


Gridview.aspx

<asp:GridView ID="GridView1" runat="server" Style="z-index: 103; left: 232px; position: absolute;
        top: 225px" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <Columns>
           <asp:TemplateField HeaderText="Image">
                 <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?id="+ Eval("ImgId")  %>'/>
                 </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>


GridView.cs


GridView.cs

public void Load_GridData()
{
    con.Open();
    string sql = "Select ImgId,Id,ImageName,Image from Pictures";
    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    DataTable dt = new DataTable();
    da.Fill(dt);   // fill the dataset
    GridView1.DataSource = dt; // give data to GridView
    GridView1.DataBind();
    con.Close();
    GridView1.Attributes.Add("bordercolor", "black");
}


Handler.ascx


Handler.ascx

using System;
using System.Web;
using System.Data.SqlClient;
public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\omar\Documents\Visual Studio 2005\WebSites\AJAXEnabledWebSite7\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        //  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                SqlCommand com = new SqlCommand("Select  Image From Pictures Where ImgId=@imgid", con);
        com.Parameters.Add("@imgid", System.Data.SqlDbType.Int).Value = context.Request.QueryString["id"];
        con.Open();
        SqlDataReader dReader = com.ExecuteReader();
        dReader.Read();
        com.Prepare();

        context.Response.BinaryWrite((byte[])dReader["Image"]);
        con.Close();
        //context.Response.End(); 
    }

    public bool IsReusable {
        get {
            return false;
        }
    }


推荐答案


这篇关于在网格视图中显示保存的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:40
查看更多