自定义分页控件

a:前台页面代码 DataList进行自定义分页 主要是在FooterTemplate里自定义
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="include_usercontrol_upindex_Test_Default2" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>分页控件</title>
    <link href="/include/css/newcss.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" Style="position: relative" Height="59px" RepeatColumns="2" Width="100%" OnItemDataBound="DataList1_ItemDataBound">
            <ItemTemplate>
                <div style="text-align: left">
                    <asp:Label ID="Label1" runat="server" Style="position: relative" Text='<%# Eval("UInfoSubject") %>'></asp:Label>&nbsp;</div>
            </ItemTemplate>
            <FooterTemplate>
                <asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
            </FooterTemplate>
        </asp:DataList>&nbsp;</div>
    </form>
</body>
</html>

b:后台C#代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class include_usercontrol_upindex_Test_Default2 : System.Web.UI.Page
{
    DataAction action = DataAction.getInstance();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            setDataBind();
        }       
    }
    public DataTable getDataTB()
    {
        string sql = "select Subject from Tinfo";
        DataTable tb = action.executeDataAdapter(sql);
        Session["pageTB"] = tb;
        return tb;
    }
    public int getPateIndex()
    {
        return  Request.QueryString["curPage"] == null ? 0 : Convert.ToInt32(Request.QueryString["curPage"]);
      
    }
    public PagedDataSource getPageDate(int curID)
    {
        DataTable tb = Session["pageTB"] == null ? this.getDataTB() : (DataTable)Session["pageTB"];
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = tb.DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = 16;
        pds.CurrentPageIndex = curID;
        return pds;
      
    }
    public void setDataBind()
    {
        this.DataList1.DataSource=this.getPageDate(this.getPateIndex());
        this.DataList1.DataBind();
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            int count = this.getPageDate(0).PageCount;
            PlaceHolder ph = e.Item.FindControl("ph") as PlaceHolder;
            for (int i = 0; i <count; i++)
            {
                if (i == this.getPateIndex())
                {
                    Label lb = new Label();
                    lb.Text =Convert.ToString(i+1);
                    lb.ForeColor = System.Drawing.Color.Red;
                    lb.CssClass = "IndexInfo_a_original";
                    lb.Font.Bold = true;
                    ph.Controls.Add(lb);
                }
                else
                {
                    HyperLink hl = new HyperLink();
                    hl.Text = Convert.ToString(i + 1);
                    hl.ToolTip = "当前是第【" + hl.Text + "】页";
                    hl.CssClass = "IndexInfo_a";
                    hl.ForeColor = System.Drawing.Color.Black;
                    string curUrl = Request.Url.ToString();
                    if (curUrl.IndexOf("?curPage=") != -1)
                    {
                        curUrl = curUrl.Substring(0, curUrl.LastIndexOf("?curPage="));
                    }
                    hl.NavigateUrl = curUrl + "?curPage=" + i.ToString();                   
                    ph.Controls.Add(hl);
                   
                }
                Literal l = new Literal();
                l.Text = " ";
                ph.Controls.Add(l);
            }

        }

    }
}

posted @ 2007-06-01 12:00  Microbar  阅读(310)  评论(1编辑  收藏  举报