GridView中显示数据库里的图片
来源:互联网 发布:公路平面交叉优化设计 编辑:程序博客网 时间:2024/05/16 01:52
GridView中显示数据库里的图片
很多人开始有这个疑问,GridView控件中的ImageField没有DataField属性,那么如何才能绑定到SQL Server中的Image Field?自从DynamicImage控件从beta2中消失后,这就成了个问题。但是,ASP.NET2.0随之也给我们带来了另外一种解决方案,那就是方便地利用HttpHandler(.ashx)动态显示数据库中的图片,这点在VS2005中提供了PersonalWebSite等模版中已经给出方案:通过ashx动态获取数据库中的某条图片数据,然后在GridView等控件的自定义模版中安置一个Image控件,并设置Image控件的ImageUrl属性为类似 XXX.ashx?photoId=1 即可显示图片。
下面为Handler.ashx的代码:
<%@ WebHandler Language="C#" Class="Handler" %>using System;using System.Data;using System.Data.SqlClient;using System.IO;using System.Web;using System.Configuration;public class Handler : IHttpHandler ...{ public bool IsReusable ...{ get ...{ return true; } } public void ProcessRequest(HttpContext context) ...{ // Set up the response settings context.Response.ContentType = "image/jpeg"; context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.BufferOutput = false; int photoId = -1; Stream stream = null; if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "") ...{ photoId = Convert.ToInt32(context.Request.QueryString["PhotoID"]); stream = GetPhoto(photoId); } const int buffersize = 1024 * 16; byte[] buffer = new byte[buffersize]; int count = stream.Read(buffer, 0, buffersize); while (count > 0) ...{ context.Response.OutputStream.Write(buffer, 0, count); count = stream.Read(buffer, 0, buffersize); } } public Stream GetPhoto(int photoId) ...{ SqlConnection myConnection = new SqlConnection( ConfigurationManager.ConnectionStrings["Personal"].ConnectionString); SqlCommand myCommand = new SqlCommand ("SELECT [BytesOriginal] FROM [Photos] WHERE [PhotoID]=@PhotoID", myConnection); myCommand.CommandType = CommandType.Text; myCommand.Parameters.Add(new SqlParameter("@PhotoID", photoId)); myConnection.Open(); object result = myCommand.ExecuteScalar(); try ...{ return new MemoryStream((byte[])result); } catch (ArgumentNullException e) ...{ return null; } finally ...{ myConnection.Close(); } }}
然后在GridView中自定义模版列:
<asp:TemplateField> <ItemTemplate> <asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?PhotoID=" + Eval("PhotoID") %>' /> </ItemTemplate></asp:TemplateField>
这样即可在GridView中动态显示数据库中image类型图片数据了。其实VS2005自带的Stater Kit很有用的,另外,还有其他几个Club Web Site Starter Kit等,都可以在asp.net上载过来看看。
- GridView中显示数据库里的图片
- GridView中显示数据库里的图片
- GridView中显示数据库里的图片
- GridView中显示数据库里的图片
- GridView中显示数据库里的图片
- GridView中显示数据库里的图片
- GridView中显示数据库里的图片
- GridView中显示数据库里的图片
- 存在数据库里的图片直接显示到PictureBox中
- GridView显示数据库图片
- gridview 显示sdcard中图片的问题
- 在GridView中显示图片
- 在GridView中显示图片
- 在gridview中显示图片
- B6在Picturebox显示数据库里的二进制的图片
- 如何在GridView中设置显示图片的大小
- Android中利用GridView显示SD卡的图片
- 在GridView中显示SD卡上的全部图片
- Excel动态图表
- Linux下如何用date获取当前日期的前一天
- oracle10g 初始化参数说明
- 5月15日 毕业设计检查
- 通过 IBM WESB 和 IBM WebSphere DataPower SOA Appliances 使 SOA 变成现实
- GridView中显示数据库里的图片
- Winform异步调用WebService
- 使用 ref 和 out 传递数组
- 我想握住你的手(最后更新5.21 10:32)
- visual studio 2005快捷键
- 使用 PHP 搭建自己的视频点播网站,第 1 部分:基础工作
- 中文排版CSS心得
- 看一次哭一次 ---- 战士大哭跪地:求求你们让我再去救一个...
- GDI+ 在Delphi程序的应用 -- 制作水印效果图片