有关ashx后缀名的文件请看 消除图片在ie中缓存而无法更新的问题 相关文章
从数据库中读取和保存图片请看 写入和读取图片(c# asp。net sqlserver) 相关文章
下面利用ashx文件可以方便实现从数据库中读取图片并显示在datagrid当中
//---------------------------------------BindImage.aspx ----------------------------------------
<%@ Page language="c#" Codebehind="BindImage.aspx.cs" AutoEventWireup="false" Inherits="ShowImage.BindImg" %>
<HTML>
<HEAD>
<title>BindImg</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="MyDataGrid" runat="server" AutoGenerateColumns="False" Width="632px">
<AlternatingItemStyle BackColor="Beige"></AlternatingItemStyle>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Photo">
<ItemTemplate>
<img src='<%# "GetImage.ashx?ID="+DataBinder.Eval(Container.DataItem,"EmployeeID")%>'>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="title" HeaderText="Title"></asp:BoundColumn>
</Columns>
</asp:DataGrid></FONT>
</form>
</body>
</HTML>
//------------------------------------BindImage.aspx.cs ---------------------------------------------
P>using System;
using System.Data;
using System.Drawing;
using System.Web; using System.Data.SqlClient;
namespace ShowImage
{
///
/// BindImg 的摘要说明。
///
public class BindImage: System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
SqlConnection conn = new SqlConnection(@"Server=shoutor\mydb;database=northwind;uid=sa;Pwd=shoutor");
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("select employeeID,lastname,firstname,title from employees",conn);
SqlDataReader reader = cmd.ExecuteReader();
MyDataGrid.DataSource = reader;
MyDataGrid.DataBind();
}
finally
{
conn.Close();
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
//-----------------------------GetImage.ashx ---------------------------------------
<%@ WebHandler Language="C#" Class="ShowImage.GetImage" CodeBehind="GetImage.ashx.cs" %>
//--------------------------------GetImage.ashx.cs----------------------------------
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace ShowImage
{
///
/// GetImg 的摘要说明。
///
public class GetImage : IHttpHandler
{