据说在GridView 控件中显示图片有几种方法,下面说一种
先看代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|/MYSQL.mdf;Integrated Security=True;User Instance=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [URL] FROM [tupian]"></asp:SqlDataSource>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" PageSize="1">
<EmptyDataTemplate>
</EmptyDataTemplate>
<Columns>
<asp:ImageField DataImageUrlField="URL" DataImageUrlFormatString="tupian/{0}" >
</asp:ImageField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
重要的是这行:
<asp:ImageField DataImageUrlField="URL" DataImageUrlFormatString="tupian/{0}" >
</asp:ImageField>
DataImageUrlField属性表明要绑定的字段,DataImageUrlFormatString属性指明了图片的真是路径,因为数据库一般只保存图片的文件名,要自己写明具体的位置,{0}是占位符,代表"URL"这个字段值在字符串中的位置.