这是一个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的朋友多交流