ASP.NET - 在 GridView 中显示图像和 pdf
我想在 asp:GridView 中显示“图像”列。这个想法是提供图像的缩略图以及实际尺寸图像的链接。对于某些行,这也可以是 PDF 文档。我想要 PDF 的链接。 PDF 或图像存储在 SQL 数据库中。
现在我在处理程序 (.ashx) 文件中出现错误:
“当不存在数据时,读取尝试无效。”
这是我的代码:
ASP:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="assessment_id" HeaderText="assessment_id"
InsertVisible="False" ReadOnly="True"
SortExpression="assessment_id" />
<asp:BoundField DataField="a_mime" HeaderText="a_mime" SortExpression="a_mime" />
<asp:TemplateField HeaderText="a_data">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?ID=" + Eval("ID")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [assessment_id], [a_data], [a_mime] FROM [Assessments]">
</asp:SqlDataSource>
处理程序ASHX:
<%@ WebHandler Language="C#" Class="Handler" %>
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
// Create SQL Command
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select a_data from Assessments where assessment_id =@ID";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
ImageID.Value = Convert.ToInt32(context.Request.QueryString["assessment_id"]);
cmd.Parameters.Add(ImageID);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["a_data"]);
dReader.Close();
con.Close();
}
如果可能,请帮助我。如果这很耗时,请提供示例或博客文章的链接。
I want to display in an asp:GridView an "Images" column. The idea is to provide a thumbnails of the image with link to real size image. For some rows, this may alternatively be a PDF document. I'd like the link to be to the PDF. The PDF or image are stored in a SQL database.
Now I have error in Handler (.ashx) file:
"Invalid attempt to read when no data is present."
This is my code :
ASP:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="assessment_id" HeaderText="assessment_id"
InsertVisible="False" ReadOnly="True"
SortExpression="assessment_id" />
<asp:BoundField DataField="a_mime" HeaderText="a_mime" SortExpression="a_mime" />
<asp:TemplateField HeaderText="a_data">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?ID=" + Eval("ID")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [assessment_id], [a_data], [a_mime] FROM [Assessments]">
</asp:SqlDataSource>
The Handler ASHX:
<%@ WebHandler Language="C#" Class="Handler" %>
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
// Create SQL Command
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select a_data from Assessments where assessment_id =@ID";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
ImageID.Value = Convert.ToInt32(context.Request.QueryString["assessment_id"]);
cmd.Parameters.Add(ImageID);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["a_data"]);
dReader.Close();
con.Close();
}
If it is possible, please help me. If it's time-consuming, please provide a link to an example or blog post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于
没有数据时读取无效尝试
的错误原因是 DataReader 不包含任何记录(给定 ID 没有图像)。考虑将您的代码更改为:
一些改进建议
如果可能,请考虑检查给定的评估是否具有所需的图像或 PDF。也许添加一个 where 子句来以某种方式确定该记录是否需要显示 PDF 或图像场景。
我还可以建议您将 SQL 语句转换为存储过程,如上所述,并修改您的 SqlDataSource,如下所示:
现在,在 Gridview 上,您可以确定是要渲染图像还是 PDF 链接。
将 Gridview 设置为具有一个 ItemDataBound 事件,该事件调用您可以编写的新方法。
在代码隐藏中,您可以确定要在该占位符中呈现哪个 Web 控件。
然后,在
.ashx
中,您必须读取查询字符串参数来确定要输出的内容:图像、缩略图或存储在数据库中的 PDF 文档。The reason for the error about
Invalid attempt to read when no data is present
is because the DataReader doesn't contain any records (no image for the given ID).Consider changing your code to:
A few suggestions for improvement
Consider checking, where possible, that the given Assessment has the image or PDF required. Perhaps add a where clause to determine, somehow, whether this record needs to display the PDF or image scenario.
May I suggest also that you convert your SQL statements into a stored procedure, as above, and modify your SqlDataSource as such:
Now on your Gridview, you can determine whether you want to render an image or a PDF link.
Setup your Gridview to have an ItemDataBound event that calls a new method that you can write.
In your code-behind, you can then determine which webcontrol to render in that placeholder.
Then in your
.ashx
, you'll have to read the querystring parameters to determine what to output: an image, a thumbnail image, or the PDF document stored in the database.