用ASP.net 的GridView控件分页显示图片,并历遍给定的文件夹中的文件绑定到GridView控...

       在做相册时,有时候由于图片较多,往往需要进行分布显示图片。使用ASP.net分页显示的方法有很多种,本人喜欢使用GridView控件,因为GridView控件本身具有强大的分页功能,无需太多的编程,使用起来非常方便,下面是本 网站中组织图库时使用到的自做的自定义控件,贴出来供大家参考。其方法是:

        (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()绑定
    }
}
 

转载于:https://www.cnblogs.com/tzwhx/archive/2008/10/04/1303799.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值