这是一个gridview其中的两行,前4列均为文本字段,如用户名、中文名、密码等,第5列是oracle数据库中存储的longrow字段,放的是用户签名扫描图象,用于系统中的电子签名,今天要说的是如何在gridview中显示数据库中的大对象图象(如上图中的中文签名)。
我最初的思路是gridview中有响应读取并显示图象的field类型,没错,的确有一个imagefield,但是该字段类型是接受一个图象URL的,但我要做的是将数据库中的图象数据动态获取,并且动态显示在gridview中,因为曾经对asp比较熟悉,并用asp完成过类似任务。所以习惯性的使用了以前的编程经验,说一下思路,首先创建包含gridview的页面,与相应的datasource控件绑定后,gridview只生成除大对象字段以外的其他列,所以要手动加一个imagefield进来,用于显示签名图象,假定表中的签名图象字段名为qm,则把imagefield的DataImageUrlField设置为USERNAME(与该字段绑定),USERNAME为主键,DataImageUrlFormatString设置为getimage.aspx?username={0},gridview设置完毕,新建一个web窗体为getimage.aspx,该文件用于根据username获取签名图象,并用Response.BinaryWrite方法输出。
代码列举如下:
getimage.aspx
Imports System.Data.OracleClient
Partial Class getimage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim username As String
username = Request("username")
Dim conn As OracleConnection
Dim cmd As OracleCommand
Dim ConnectionStringSettings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("OP_ConnectionString")
'Response.Write(ConnectionStringSettings.ConnectionString)
conn = New OracleConnection(ConnectionStringSettings.ConnectionString)
cmd = New OracleCommand
With cmd
.Connection = conn
.CommandText = "select qm from user where username=:username"
.Parameters.Add("username", OracleType.VarChar).Value = username
End With
Dim image() As Byte
Try
With cmd
.Connection.Open()
image = .ExecuteScalar()
.Connection.Close()
Response.BinaryWrite(image)
End With
Catch ex As Exception
Response.TransmitFile(Server.MapPath("noimage.jpg"))
End Try
End Sub
End Class
包含Gridview控件的页面中对Gridview的声明
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource_User" ForeColor="#333333" Width="700px" DataKeyNames="username" EmptyDataText="没有用户信息" Font-Size="Small" PageSize="8" BorderStyle="Dotted" BorderWidth="1px" >
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="USERNAME" HeaderText="用户名" SortExpression="USERNAME" ReadOnly="True">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="DUTY" HeaderText="单位" SortExpression="DUTY">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="NOTES" HeaderText="姓名" SortExpression="NOTES">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="PASSWORD" HeaderText="密码" SortExpression="PASSWORD">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:ImageField DataImageUrlField="username" DataImageUrlFormatString="getimage.aspx?username={0}"
HeaderText="签名" ReadOnly="True">
<ControlStyle Width="150px" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:ImageField>
<asp:CommandField ButtonType="Button" ShowEditButton="True">
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="Lime" BorderWidth="1px" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
以上涉及到的两个aspx文件必须放在同一目录,本例使用的oracle数据库。
asp.net 2 让我有一中越用越爽的感觉,操作数据库成了很愉快的事~~~
希望能和研究asp.net 2的朋友多交流