自定义控件之翻页控件

关于翻页一直很让我挠头,今天总结一个比较经典的。

 

1.PageControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PageControl.ascx.cs" Inherits="UserContol.PageControl" %>
<asp:HiddenField ID="ClickedPage" runat="server">
</asp:HiddenField>
<asp:HiddenField ID="HiddenTotalRecordCount" runat="server">
</asp:HiddenField>
<asp:HiddenField ID="HiddenRecordPerPage" runat="server">
</asp:HiddenField>
<div id="Style1" runat="server">
(共<asp:Literal ID="RecordCount" runat="server"></asp:Literal>条记录)
<asp:Button  ID="Prev" Text="<-" Visible="false" runat="server" />
<asp:LinkButton ID="Prev5" Visible="false" runat="server">...</asp:LinkButton>
    <asp:Repeater ID="PageButtons" runat="server" EnableViewState="false">
    <ItemTemplate>
    <asp:Button Visible="false" runat="server" />
    <asp:Literal Visible="false" runat="server"></asp:Literal>
    </ItemTemplate>
    </asp:Repeater>
<asp:LinkButton ID="Next5" Visible="false" runat="server">...</asp:LinkButton>
<asp:Button ID="Next" Text="下一页->" Visible="false" runat="server" />
共<asp:Literal ID="LabelTotalPage" runat="server"></asp:Literal>页
<asp:Literal ID="Literal1" runat="server">到</asp:Literal>
<asp:TextBox ID="PageNo" runat="server"></asp:TextBox>
<asp:Literal ID="Literal2" runat="server">页</asp:Literal>
<asp:Button ID="GO" Text="确定" runat="server" />
</div>

<div id="Style2" runat="server">
(共<asp:Literal ID="RecordCount2" runat="server"></asp:Literal>条记录)
<asp:Literal ID="CurrentPage2" runat="server"></asp:Literal>/<asp:Literal ID="LabelTotalPage2" runat="server"></asp:Literal>页
<asp:Button ID="Prev2" Text="上一页" Visible="false"  CssClass="invalid" runat="server" />
<asp:Button ID="Next2" Text="下一页" Visible="false"  CssClass="invalid" runat="server" />
</div>

<script type="text/javascript">
    function ChangePage$<%=this.ClientID%>(page)
    {
        aspnetForm.<%=ClickedPage.ClientID%>.value=page;
    }
</script>


2.PageControl.ascx.cs文件

 

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;
using System.Web.Services.Description;
using System.Collections.Generic;

namespace UserContol
{
 public partial class PageControl : System.Web.UI.UserControl
    {

  public int CurrentPage
  {
   get
   {
    try
    {
     return int.Parse(this.ClickedPage.Value);
    }
    catch
    {
     return 0;
    }
   }
   set
   {
    this.ClickedPage.Value = value.ToString();
   }
  }


  public int TotalRecordCount
  {
   get
   {
    try
    {
     return int.Parse(this.HiddenTotalRecordCount.Value);
    }
    catch
    {
     return 0;
    }
   }
   set
   {
    this.HiddenTotalRecordCount.Value = value.ToString();
   }
  }

  public int RecordPerPage
  {
   get
   {
    try
    {
     return int.Parse(this.HiddenRecordPerPage.Value);
    }
    catch
    {
     return 0;
    }
   }
   set
   {
    this.HiddenRecordPerPage.Value = value.ToString();
   }
  }

  public int TotalPage
  {
   get
   {
    return (int)Math.Ceiling((float)TotalRecordCount / (float)RecordPerPage);
   }
  }


  //"Simple"/"Full"
  private String style;
  public String ButtonStyle
  {
   get
   {
    return this.style;
   }
   set
   {
    this.style = value;
   }
  }

  public delegate void PageChangeHandler(int page);

  public event PageChangeHandler pageChanged;

  public void Paint()
  {
   List<ButtonPack> pageButtons = new List<ButtonPack>();
   MakePageList(RecordPerPage, TotalRecordCount, CurrentPage, pageButtons);
   this.PageButtons.ItemDataBound += new RepeaterItemEventHandler(DataList1_ItemDataBound);
   this.PageButtons.DataSource = pageButtons;
   this.PageButtons.DataBind();
   this.CurrentPage2.Text = (this.CurrentPage + 1).ToString();
   this.RecordCount.Text = this.TotalRecordCount.ToString();
   this.RecordCount2.Text = this.TotalRecordCount.ToString();

   if (this.TotalRecordCount == 0)
   {
    this.PageNo.Visible = false;
    this.GO.Visible = false;
    this.Literal1.Visible = false;
    this.Literal2.Visible = false;
   }
   else
   {
    this.PageNo.Visible = true;
    this.GO.Visible = true;
    this.Literal1.Visible = true;
    this.Literal2.Visible = true;
   }
  }

  protected void Page_Load(object sender, EventArgs e)
  {

   if ("Simple".Equals(style))
   {
    this.Style1.Visible = false;
    this.Style2.Visible = true;

    if (CurrentPage < 0)
    {
     CurrentPage = 0;
    }

    if (CurrentPage > TotalPage)
    {
     CurrentPage = TotalPage;
    }
   }
   else
   {
    this.Style1.Visible = true;
    this.Style2.Visible = false;

    try
    {
     CurrentPage = int.Parse(PageNo.Text) - 1;
     PageNo.Text = "";
    }
    catch
    {
    }

    if (CurrentPage < 0)
    {
     CurrentPage = 0;
    }

    if (CurrentPage > TotalPage)
    {
     CurrentPage = TotalPage;
    }

   }

   if (this.IsPostBack)
   {
    //动态控件事件触发
    if (this.Prev5.UniqueID.Equals(Request["__EVENTTARGET"]))
    {
     pageChanged.Invoke(CurrentPage);
    }
    if (this.Next5.UniqueID.Equals(Request["__EVENTTARGET"]))
    {
     pageChanged.Invoke(CurrentPage);
    }

    foreach (string param in Request.Params.Keys)
    {
     if (param.Equals(this.Prev.UniqueID))
     {
      pageChanged.Invoke(CurrentPage);
     }
     if (param.Equals(this.Prev2.UniqueID))
     {
      pageChanged.Invoke(CurrentPage);
     }

     if (param.StartsWith(this.PageButtons.UniqueID))
     {
      pageChanged.Invoke(CurrentPage);
     }

     if (param.Equals(this.Next.UniqueID))
     {
      pageChanged.Invoke(CurrentPage);
     }
     if (param.Equals(this.Next2.UniqueID))
     {
      pageChanged.Invoke(CurrentPage);
     }
     if (param.Equals(this.GO.UniqueID))
     {
      pageChanged.Invoke(CurrentPage);
     }
    }
   }
   else
   {
   }


  }

  void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
  {
   List<ButtonPack> datat = this.PageButtons.DataSource as List<ButtonPack>;

   Button pageButton = ((Button)e.Item.Controls[1]);
   Literal pageLiteral = ((Literal)e.Item.Controls[3]);

   if (datat[e.Item.ItemIndex].currentPage)
   {
    pageLiteral.Visible = true;
    pageButton.Visible = false;
    pageLiteral.Text = datat[e.Item.ItemIndex].Text;
   }
   else
   {
    pageLiteral.Visible = false;
    pageButton.Visible = true;
    pageButton.Text = datat[e.Item.ItemIndex].Text;
    pageButton.OnClientClick = getChangePageJs(datat[e.Item.ItemIndex].Page);
   }
  }

  private void MakePageList(int iLineCount, int iTotal, int iCurrentPage, List<ButtonPack> pageButtons)
  {
   this.Prev.Visible = false;
   this.Prev2.Visible = false;
   this.Prev5.Visible = false;
   this.Next5.Visible = false;
   this.Next.Visible = false;
   this.Next2.Visible = false;

   int width = 9;
   int middle = 4;

   this.LabelTotalPage.Text = TotalPage.ToString();
   this.LabelTotalPage2.Text = TotalPage.ToString();

   int start = iCurrentPage;
   if (start > width)
   {
    start = width;
   }

   int end = TotalPage - iCurrentPage;
   if (end > width)
   {
    end = width;
   }

   while (start + end > width)
   {
    if (iCurrentPage <= width && start > middle)
    {
     start--;
    }
    else if (TotalPage - iCurrentPage <= width && end > middle)
    {
     end--;
    }
    else
    {
     if (start > end)
     {
      start--;
     }
     else
     {
      end--;
     }
    }
   }
   if (TotalPage - iCurrentPage <= middle)
   {
    start++;
   }

   if ((iCurrentPage >= 0 && iCurrentPage <= TotalPage - 1))
   {

    if (iCurrentPage - 1 >= 0 && iCurrentPage - 1 <= TotalPage - 1)
    {
     Prev.Visible = true;
     Prev2.Visible = true;
     Prev.OnClientClick = getChangePageJs(iCurrentPage - 1);
     Prev2.OnClientClick = getChangePageJs(iCurrentPage - 1);
    }

    if (iCurrentPage - start - 1 >= 0)
    {
     Prev5.Visible = true;
     Prev5.OnClientClick = getChangePageJs(iCurrentPage - start - 1);
    }

    for (int i = iCurrentPage - start; i <= iCurrentPage + end; i++)
    {
     if (i >= 0 && i <= TotalPage - 1)
     {
      ButtonPack pageButton = new ButtonPack();
      pageButton.currentPage = i == iCurrentPage;
      pageButton.Text = "" + (i + 1);
      pageButton.Page = i.ToString();
      pageButtons.Add(pageButton);
     }
    }
    if (iCurrentPage + end + 2 <= TotalPage)
    {
     Next5.Visible = true;
     Next5.OnClientClick = getChangePageJs(iCurrentPage + end + 1);
    }
    if (iCurrentPage + 1 <= TotalPage - 1)
    {
     Next.Visible = true;
     Next2.Visible = true;
     Next.OnClientClick = getChangePageJs(iCurrentPage + 1);
     Next2.OnClientClick = getChangePageJs(iCurrentPage + 1);
    }
   }
  }
  private String getChangePageJs(int page)
  {
   return "ChangePage$" + this.ClientID + "(" + page + ")";
  }
  private String getChangePageJs(String page)
  {
   return "ChangePage$" + this.ClientID + "(" + page + ")";
  }

    }

    class ButtonPack
    {
        public bool currentPage;
        public String Text;
        public String Page;
    }
}

 

3.调用的地方:test.aspx

先注册,再调用:

<%@ Register Src="~/PageControl.ascx" TagPrefix="Page" TagName="PagerControl" %>

<Page:PagerControl ID="Page1" OnpageChanged="onpageChanged" runat="server" />

 

4.test.aspx.cs

// 翻页初始化
this.Page1.CurrentPage = 0;
this.Page1.TotalRecordCount = 0;
this.Page1.RecordPerPage = 10;
this.Page1.Paint();

 

UserManagement userManagement = new UserManagement ();
            list = userManagement .GetInfoByUserID(
                this.Page1.RecordPerPage,
                this.Page1.CurrentPage,
                ref total,
                (string)ViewState["Status"],
                UserID);
            this.Page1.TotalRecordCount = total;
            this.Page1.Paint();

 

5.后台的存储过程:sp_GetUserInfo

CREATE PROC [dbo].[sp1_Online_GetOrderInfo](
@PageSize int,
@CurrPage int,
@Total int OUTPUT,

@Status nvarchar(1),
@UserID int
)
AS

SET LOCK_TimeOut 1000
DECLARE @RetCode int

 

SELECT @Total = COUNT(1)
FROM table1 as o with(nolock)
WHERE
 (@Status is null OR o.Status = @Status )
AND o.UserID = @UserID

 SELECT 
 a,

 b,

 c,
rowno
FROM
(
 SELECT
 o.a,

 i.b,

 d.c,
 ROW_NUMBER()OVER(
 ORDER BY o.id desc
 ) as rowno
 FROM table1  as o with(nolock)
 INNER JOIN table2      as i with(nolock) on i.id= o.id
 INNER JOIN table3      as d with(nolock) on d.id= o.id
 WHERE
 (@Status  is null OR o.Status = @Status)
 AND o.UserID = @UserID
) t1
WHERE t1.rowno - 1 >= (@CurrPage*@PageSize)
AND t1.rowno - 1 < ((@CurrPage+1)*@PageSize)
ORDER BY t1.rowno

 

6.调用存储过程的文件(数据层)UserDB.cs

internal List<UserEntity> GetInfoByUserID(
            int pageSize,
            int currPage,
            ref int total,
            string Status,
            int userID)
        {
            InnDBObject innDBObject = new InnDBObject();

            DbCommand dbCommand = innDBObject.GetStoredProcCommand("sp_GetUserInfo");
            innDBObject.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
            innDBObject.AddInParameter(dbCommand, "@CurrPage", DbType.Int32, currPage);
            innDBObject.AddInParameter(dbCommand, "@Status", DbType.String, Status);
            innDBObject.AddInParameter(dbCommand, "@UserID", DbType.Int32, userID);
            innDBObject.AddOutParameter(dbCommand, "@Total", DbType.Int32,4);
            DataSet dataSet = innDBObject.ExecuteDataSet(dbCommand);

            total = (int)innDBObject.GetParameterValue(dbCommand, "@Total");

            return GetListFromDataSet(dataSet);            
        }

 

internal static List<UserEntity> GetListFromDataSet(DataSet dataSet)
        {
            if (dataSet.Tables.Count > 0)
            {
                List<UserEntity> retList = new List<UserEntity>();

                foreach (DataRow dr in dataSet.Tables[0].Rows)
                {
                    UserEntity user = new UserEntity();
                    user.DetailEntity = new DetailEntity();
                    user.DetailEntity.a = InnDBObject.setValue<int>(dr, "a");
                    user.DetailEntity.b = InnDBObject.setValue<string>(dr, "b");
                    user.c  = InnDBObject.setValue<int>(dr, "c");
                    retList.Add(user);
                }
                return retList;

            }
            else
            {
                return new List<UserEntity>();
            }
        }

 

7.业务层画面

public List<UserEntity> GetInfoByUserID(
            int pageSize,
            int currPage,
            ref int total,
            string Status,
            int userID)
        {
            UserDB userDB = new UserDB();
            return userDB.GetInfoByUserID(pageSize, currPage, ref total, Status, userID);

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值