用户控件打造分页控件

首先新建一个UserControl:

Pager.ascx:

 

代码
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Pager.ascx.cs" Inherits="WebApplication1.Pager" %>
<table id="pager" border="0" cellpadding="0" cellspacing="0" width="100%">
    
<tr>
        
<td>
            
<asp:LinkButton ID="lbtnFirstLink" runat="server" CausesValidation="false" OnClick="ClickEvent">首页</asp:LinkButton>
            
<asp:LinkButton ID="lbtnPrevPage" runat="server" CausesValidation="false"  OnClick="ClickEvent">上一页</asp:LinkButton>
    
            
<asp:Repeater ID="rptNum" runat="server" OnItemCommand="rptNum_ItemCommand">
                
<ItemTemplate>
    
<asp:LinkButton ID="lbtnNum" runat="server" CausesValidation="false" CommandArgument='<%# Container.DataItem %>'
    CssClass='
<%# Convert.ToInt32(Container.DataItem)==PageIndex?"num_yellow":"num_blue"%>' Width="15"><%# Container.DataItem %>
</asp:LinkButton>
                
</ItemTemplate>
            
</asp:Repeater>
      
            
<asp:LinkButton ID="lbtnNextPage" runat="server" CausesValidation="false"  OnClick="ClickEvent">下一页</asp:LinkButton>
            
<asp:LinkButton ID="lbtnLastPage" runat="server" CausesValidation="false"  OnClick="ClickEvent">尾页</asp:LinkButton>
            
<asp:HiddenField ID="hfBindName" runat="server" />
        
</td>
    
</tr>
</table>

 

Pager.ascx.cs:

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Reflection;

namespace WebApplication1
{
    
public partial class Pager : System.Web.UI.UserControl
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
                BindPager();
            }
        }

        
/// <summary>
        
/// 点击数字列表通过反射运行时调用父页面的方法
        
/// </summary>
        
/// <param name="source"></param>
        
/// <param name="e"></param>
        protected void rptNum_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            ReBindData(e.CommandArgument.ToString());
        }

        
/// <summary>
        
/// 通过反射运行时调用父页面的方法
        
/// </summary>
        
/// <param name="pageIndex"></param>
        protected void ReBindData(string pageIndex)
        {
            PageIndex 
= int.Parse(pageIndex);
            
object obj = base.Parent is HtmlForm ? this.Page : base.Parent;
            MethodInfo mi 
= obj.GetType().GetMethod(BindName);
            mi.Invoke(obj, 
null);
            BindPager();
        }

        
/// <summary>
        
/// 绑定分页
        
/// </summary>
        protected void BindPager()
        {
            PageCount 
= Count % PageSize == 0 ? (Count / PageSize) : (Count / PageSize) + 1;
            SetButtonEnable(PageCount);
            BindPageNum();
        }

        
/// <summary>
        
/// 计算出数字列表
        
/// </summary>
        protected void BindPageNum()
        {
            
int start = 1, end = 10;

            
if (PageCount < end)
            {
                end 
= PageCount;
            }
            
else
            {
                start 
= PageIndex > 5 ? PageIndex - 5 : start;

                
int result = (start + 9- PageCount;

                
if (result > 0)
                {
                    end 
= PageCount;
                    start 
= start - result;
                }
                
else
                {
                    end 
= start + 9;
                }
            }

            ReBindNum(start, end);

        }

        
/// <summary>
        
/// 绑定分页数字列表
        
/// </summary>
        
/// <param name="start"></param>
        
/// <param name="end"></param>
        protected void ReBindNum(int start, int end)
        {
            
int capacity = 10;

            
if (PageCount < 10)
                capacity 
= PageCount;

            
int[] rows = new int[capacity];

            
int index = 0;

            
for (int i = start; i <= end; i++)
            {
                rows[index] 
= i;
                index
++;
            }

            rptNum.DataSource 
= rows;
            rptNum.DataBind();
        }

        
/// <summary>
        
/// 设置First,Previous,Next,Last按钮的启用状态和页面跳转方式
        
/// </summary>
        
/// <param name="PageCount"></param>
        protected void SetButtonEnable(int PageCount)
        {
            lbtnFirstLink.Enabled 
= PageIndex != 1;
            lbtnPrevPage.Enabled 
= PageIndex != 1;
            lbtnNextPage.Enabled 
= PageIndex != PageCount;
            lbtnLastPage.Enabled 
= PageIndex != PageCount;

            lbtnFirstLink.CommandArgument 
= "1";
            lbtnPrevPage.CommandArgument 
= (PageIndex - 1).ToString();
            lbtnNextPage.CommandArgument 
= (PageIndex + 1).ToString();
            lbtnLastPage.CommandArgument 
= PageCount.ToString();
        }

        
/// <summary>
        
/// 调用First,Previous,Next,Last按钮
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void ClickEvent(object sender, EventArgs e)
        {
            ReBindData(((LinkButton)sender).CommandArgument.ToString());
        }


        
private int _PageIndex;

        
/// <summary>
        
/// 当前页
        
/// </summary>
        public int PageIndex
        {
            
get
            {
                
if (_PageIndex == 0)
                {
                    _PageIndex 
= 1;
                }

                
return _PageIndex;
            }

            
set
            {
                _PageIndex 
= value;
            }
        }

        
private int _PageSize;

        
/// <summary>
        
/// 一页多少行数据
        
/// </summary>
        public int PageSize
        {
            
get
            {
                
if (_PageSize == 0)
                {
                    _PageSize 
= 1;
                }
                
return _PageSize;
            }

            
set
            {
                _PageSize 
= value;
            }
        }

        
private int _Count;

        
/// <summary>
        
/// 取出的所有记录
        
/// </summary>
        public int Count
        {
            
get
            {
                
return _Count;
            }

            
set
            {
                _Count 
= value;
            }
        }

        
/// <summary>
        
/// 通过反射运行时调用方法的名称
        
/// </summary>
        public string BindName
        {
            
get
            {
                
return hfBindName.Value;
            }

            
set
            {
                hfBindName.Value 
= value;
            }
        }

        
/// <summary>
        
/// 总页数
        
/// </summary>
        private int PageCount
        {
            
get
            {
                
if (ViewState["PageCount"!= null && ViewState["PageCount"].ToString() != "")
                    
return (int)ViewState["PageCount"];

                
return 0;
            }

            
set
            {
                ViewState[
"PageCount"= value;
            }
        }

    }
}

 

 

 

WebForm1.aspx:

 

代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<%@ Register src="Pager.ascx" tagname="Pager" tagprefix="uc1" %>

<!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>
     
<style type="text/css">
    #pager 
{color:White;}
    #pager a
{color:White;text-decoration:none; }
    .num_blue
{color:White;border:solid 1px Blue;text-align:center;}
    .num_yellow
{color:while;border:solid 1px Yellow; text-align:center;}
    
</style>
</head>
<body style="background-color:Black;color:White;" >
    
<form id="form1" runat="server">
    
<div><asp:GridView ID="gvUsers" runat="server"></asp:GridView>
    
<br />
    
<uc1:Pager ID="Pager1" runat="server" PageSize="5" />
    
</div>
    
</form>
</body>
</html>

 

 

WebForm1.aspx.cs:

 

代码
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    
public partial class WebForm1 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
                LoadData();
            }
        }

        
public void LoadData()
        {
            SqlConnection conn 
= new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnStr"]);
            DataTable dt 
= new DataTable();
            DataTable dtcount 
= new DataTable();
            SqlDataAdapter sda 
= new SqlDataAdapter("select * from tt", conn);
            sda.Fill(dtcount);
            
int index = Pager1.PageIndex;
            sda.Fill((index 
- 1* Pager1.PageSize, Pager1.PageSize, dt);
            gvUsers.DataSource 
= dt;
            gvUsers.DataBind();
            Pager1.Count 
= 23;
            Pager1.BindName 
= "LoadData";
        }
    }


}

 

转载:http://www.cnblogs.com/cyq1162/archive/2010/08/30/1812361.html

 

posted @ 2010-09-14 16:17  唔愛吃蘋果  阅读(317)  评论(0编辑  收藏  举报