我有一个存储在表中的图像,以及一个用于插入,更新和删除该表中的行的gridview(Default.aspx,Default1.apsx.cs)。但是,列之一是Varbinary(Max)数据类型。为了将该blob渲染为Gridview上的图像,我编写了一个图像处理程序(ImageHandler.ashx)。如何公开gridview名称gvHoursEmp,使其包含在ImageHandler.aspx的上下文中。它们都是通用名称空间下通用项目的成员。
// Default.aspx代码段。 HandWrittenSignature是我尝试在gvHoursEmp网格视图中正确显示的Blob。
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default1.aspx.cs" Inherits="HoursOfServiceGridView.Default"
CodeBehind="Default1.aspx.cs" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Hours of Service Employee Master</title>
<link rel="icon"
type="image/png"
href="~/images/SaveRow_16x.png" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvHoursEmp" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" ShowFooter="True" ShowHeaderWhenEmpty="True" OnRowCommand="gvHoursEmp_RowCommand"
OnRowEditing="gvHoursEmp_RowEditing" OnRowUpdating="gvHoursEmp_RowUpdating" OnRowCancelingEdit="gvHoursEmp_RowCancelingEdit"
OnRowDeleting="gvHoursEmp_RowDeleting" CellPadding="4" AllowSorting="True" ToolTip="True" ValidateRequestMode="Disabled" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" >
<%--Theme Properties--%>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" Font-Names="Arial" />
<RowStyle ForeColor="#003399" Font-Names="Arial" BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="First Name" HeaderStyle-Width="10%" ItemStyle-Width="10%" FooterStyle-Width="10%">
<ItemTemplate>
<asp:Label Text='<%# Eval("FirstName") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" Text='<%# Eval("FirstName") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFirstNameFooter" runat="server" />
</FooterTemplate>
<FooterStyle Width="10%" HorizontalAlign="Left" VerticalAlign="Middle"></FooterStyle>
<HeaderStyle Font-Names="Arial" />
<ItemStyle Width="10%" Height="10%"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mid Init">
<ItemTemplate>
<asp:Label Text='<%# Eval("MiddleInitial") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMiddleInitial" Text='<%# Eval("MiddleInitial") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMiddleInitialFooter" runat="server" />
</FooterTemplate>
<ControlStyle Height="10%" Width="20px" />
<FooterStyle Height="10%" Width="20px" Wrap="False" HorizontalAlign="Left" VerticalAlign="Middle" />
<HeaderStyle Font-Names="Arial" Height="10%" Width="20px" HorizontalAlign="Left" />
<ItemStyle Height="10%" Width="20px" Font-Names="Arial" HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label Text='<%# Eval("LastName") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" Text='<%# Eval("LastName") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLastNameFooter" runat="server" />
</FooterTemplate>
<ControlStyle Height="10%" Width="125px" />
<FooterStyle Height="10%" Width="10%" />
<HeaderStyle Font-Names="Arial" Height="10%" Width="10%" />
<ItemStyle Height="10%" Width="125px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<asp:Label Text='<%# Eval("EmployeeID") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmployeeID" Text='<%# Eval("EmployeeID") %>' runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEmployeeIDFooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Signature">
<ItemStyle HorizontalAlign="Center" Height="100px" Width="100px" Wrap="False" />
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?ID=1"+ Eval("HandWrittenSignature") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSignature" Text='<%# Eval("HandWrittenSignature") %>' runat="server" />
</EditItemTemplate>
</asp:TemplateField>
ImageHandler.ashx代码在这里:
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace HoursOfServiceGridView
{
public class ImageHandler : IHttpHandler
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context)
{
string id = context.Request.QueryString["id"];
SqlConnection sqlCon = new SqlConnection(@"Data Source=365-ETDB-
01A;Initial Catalog=TMDS_ODS;Integrated Security=True");
SqlCommand sqlCmd = new SqlCommand("SELECT HandWrittenSignature
FROM dbo.HoursOfServiceEmployee WHERE EmployeeID = @EmployeeID",
sqlCon);
sqlCmd.CommandType = CommandType.Text; SqlCmd.Parameters.AddWithValue("@EmployeeID",Convert.ToInt32(gvHoursEmp.Rows[e.RowIndex].FindControl("txtEmployeeID") as TextBox).Text.Trim());
sqlCon.Open();
byte[] pict = (byte[])sqlCmd.ExecuteScalar();
sqlCon.Close();
context.Response.ContentType = "image/bmp";
context.Response.OutputStream.Write(pict, 78, pict.Length - 78);
}
}
}
由于Gridview在ImageHandler.aspx页面的当前上下文之外,因此Gridview无法显示HandWrittenSignature Blob的图像。