(1)在GridView控件中进行分页设置,主要有以下几项设置:
AutoGenerateColumns="False" 运行时不让数据源自动生成列;
AllowPaging="True" 打开自动分布功能;
PageSize="2" 每页显示2行,我这里是每页显示2张图片;
PagerSettings FirstPageText="第一页"
LastPageText="最后页"
Mode="NextPreviousFirstLast" 显示的模式,自定义
NextPageText="下一页"
Position="TopAndBottom" 分页选择在控件上部和下部均显示;
PreviousPageText="上一页"
onpageindexchanging="GridView1_PageIndexChanging" 当选择上页、下页等换页时激发它
(2)在GridView控件中添加TemplateField模板,在TemplateField模板中添加〈ima〉,用以显示图片
(3)在后台定然字符数组,用于存入历遍给定的文件夹下的文件
(4)使用foreach循环将文件目录及图片文件名赋予数据源,并绑定到GridView控件
(5)告诉GridView1_PageIndexChanging事件,需要干什么事儿。
以下是前台源代码:
〈%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
〈!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">
〈label for="pagebody1" style="display: none">〈/label>
〈fieldset id="container">
〈legend>不分页显示图片〈/legend>
〈div style="list-style: none;">
〈ul style="width: 808px; margin: 0px">
〈li style="margin-left: 10px">
〈asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" PageSize="2" Width="800px" CellPadding="3"
CellSpacing="1" GridLines="None" onpageindexchanging="GridView1_PageIndexChanging">
〈PagerSettings FirstPageText="第一页" LastPageText="最后页" Mode="NextPreviousFirstLast"
NextPageText="下一页" Position="TopAndBottom" PreviousPageText="上一页" />
〈Columns>
〈asp:TemplateField HeaderText="独上高楼网图库">
〈ItemTemplate>
〈img alt="" src='〈%# DataBinder.Eval(Container, "DataItem.filePath", "{0}") %>' />
〈/ItemTemplate>
〈/asp:TemplateField>
〈/Columns>
〈/asp:GridView>
〈/li>
〈/ul>
〈/div>
〈/fieldset>
〈/form>
〈/body>
〈/html>
后台源代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.BindToGridView(); //调用上面的BindToGridView()绑定
}
}
protected void BindToGridView()
{
//获取文件名称
//string[] files = Directory.GetFiles(Server.MapPath(imagepath));
string[] files = Directory.GetFiles(Server.MapPath("images/"));
//建立数据表
DataTable dt = new DataTable();
//dt.Columns.Add("filename");
//dt.Columns.Add("size");
dt.Columns.Add("filePath");
foreach (string s in files)
{
DataRow dr = dt.NewRow();
FileInfo f = new FileInfo(s);
//dr["filename"] = f.Name;
//dr["size"] = f.Length;
//dr["filePath"] = imagepath + "/" + f.Name;
dr["filePath"] = "/images/" + f.Name;
dt.Rows.Add(dr);
}
//绑定显示
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.BindToGridView(); //调用上面的BindToGridView()绑定
}
}