.net分页控件

刚刚开始学.net的时候连改带写做的,现在不怎么用到....倒是觉得jquery.pager好用点...
    [DefaultProperty("Text"),
   ToolboxData(
"<{0}:CollectionPager runat=server></{0}:CollectionPager>")]
    
public class Pager : UserControl
    
{
        
const string#region const string

        
const string PAGER = "<label class=\"{1}\">{0}</label>";
        
const string LINK = "<a href=\"{1}\">{0}</a>";

        
#endregion


        
pages#region pages

        
private int maxPages;
        
private int pageIndex = 1;
        
private int recordCount = 0;
        
private int pageSize = 10;
        
private int sliderSize = 8;
        
private bool showFirstLast = true;

        
private string queryStringKey = "PageIndex";

        
#endregion


        
back/next first/last#region back/next first/last

        
private string backText = "Back";
        
private string nextText = "Next";
        
private string firstText = "First";
        
private string lastText = "Last";

        [Bindable(
true), Category("back/next first/last"), Localizable(true)]
        
public virtual string BackText
        
{
            
get return backText; }
            
set { backText = value; }
        }

        [Bindable(
true), Category("back/next first/last"), Localizable(true)]
        
public virtual string NextText
        
{
            
get return nextText; }
            
set { nextText = value; }
        }

        [Bindable(
true), Category("back/next first/last"), Localizable(true)]
        
public virtual string FirstText
        
{
            
get return firstText; }
            
set { firstText = value; }
        }

        [Bindable(
true), Category("back/next first/last"), Localizable(true)]
        
public virtual string LastText
        
{
            
get return lastText; }
            
set { lastText = value; }
        }


        
#endregion


        
control attribute#region control attribute

        [Bindable(
false), Category("Appearance")]
        
public bool ShowFirstLast
        
{
            
get
            
{
                
return showFirstLast;
            }


            
set
            
{
                showFirstLast 
= value;
            }

        }


        [Bindable(
true), Category("Appearance"), Localizable(true)]
        
public int SliderSize
        
{
            
get return sliderSize; }
            
set { sliderSize = value; }
        }


        [Bindable(
true), Category("Appearance"), Localizable(true)]
        
public virtual string QueryStringKey
        
{
            
get return queryStringKey; }
            
set { queryStringKey = value; }
        }



        [Bindable(
true), Category("Appearance"), Localizable(true)]
        
public virtual int PageIndex
        
{
            
get
            
{
                
if (Request[queryStringKey] != null)
                
{
                    
int.TryParse(Request[queryStringKey].ToString(), out pageIndex);
                }

                
return pageIndex;
            }

            
set
            
{
                pageIndex 
= value;
            }

        }


        [Bindable(
true), Category("Appearance"), Localizable(true)]
        
public virtual int RecordCount
        
{
            
get
            
{
                
return recordCount;
            }

            
set
            
{
                recordCount 
= value;
            }

        }


        [Bindable(
true), Category("Appearance"), Localizable(true)]
        
public virtual int PageSize
        
{
            
get
            
{
                
return pageSize;
            }

            
set
            
{
                pageSize 
= value;
            }

        }


        [Bindable(
false), Category("Appearance"), Localizable(true)]
        
public virtual int PageCount
        
{
            
get
            
{
                
int PageCount = 0;
                PageCount 
= RecordCount / PageSize;
                
if ((RecordCount % PageSize) != 0)
                
{
                    PageCount
++;
                }

                
if (PageCount > maxPages)
                
{
                    PageCount 
= maxPages;
                }

                
return PageCount;
            }

        }


        
#endregion


        
Separator#region Separator

        
private string pageNumbersSeparator = " - ";
        
private string backNextSeparator = "&nbsp;.&nbsp;";
        
private string numbersSectionSeparator = "&nbsp;";

        [Bindable(
false), Category("Separator"), Localizable(true)]
        
public string NumbersSectionSeparator
        
{
            
get return numbersSectionSeparator; }
            
set { numbersSectionSeparator = value; }
        }


        [Bindable(
false), Category("Separator"), Localizable(true)]
        
public string BackNextSeparator
        
{
            
get return backNextSeparator; }
            
set { backNextSeparator = value; }
        }


        [Bindable(
false), Category("Separator"), Localizable(true)]
        
public string PageNumbersSeparator
        
{
            
get return pageNumbersSeparator; }
            
set { pageNumbersSeparator = value; }
        }


        
#endregion


        
CssClass#region CssClass

        
private string pagerCssClass = "PagerCssClass";
        
private string pagerNumbersCssClass = "PagerNumbersCssClass";
        
private string pagerSectionCssClass = "PagerSectionCssClass";

        
/**//// <summary>
        
/// 跳转主样式
        
/// </summary> 

        [Bindable(true), Category("Appearance"), Localizable(true)]
        
public string PagerSectionCssClass
        
{
            
get return pagerSectionCssClass; }
            
set { pagerSectionCssClass = value; }
        }


        
/**//// <summary>
        
/// 页码显示样式
        
/// </summary>

        [Bindable(true), Category("Appearance"), Localizable(true)]
        
public string PagerNumbersCssClass
        
{
            
get return pagerNumbersCssClass; }
            
set { pagerNumbersCssClass = value; }
        }


        
/**//// <summary>
        
/// 分页主样式
        
/// </summary>

        [Bindable(true), Category("Appearance"), Localizable(true)]
        
public string PagerCssClass
        
{
            
get return pagerCssClass; }
            
set { pagerCssClass = value; }
        }



        
#endregion


        
public method#region public method
 
        
static public string UpdateQueryStringItem(System.Web.HttpRequest httpRequest, string queryStringKey, string newQueryStringValue)
        
{
            
string NewURL = httpRequest.RawUrl;
 
            
if (httpRequest.QueryString[queryStringKey] != null)
            
{
                
string OrignalSet = String.Format("{0}={1}", queryStringKey, httpRequest.QueryString[queryStringKey]);
                
string NewSet = String.Format("{0}={1}", queryStringKey, newQueryStringValue);
 
                
if (newQueryStringValue.Trim() == "")
                

                    NewURL 
= Regex.Replace(NewURL, "&" + OrignalSet, "", RegexOptions.IgnoreCase);
 
                    NewURL 
= Regex.Replace(NewURL, "?" + OrignalSet + "&""?", RegexOptions.IgnoreCase);
 
                    NewURL 
= Regex.Replace(NewURL, "?" + OrignalSet, "", RegexOptions.IgnoreCase);
                }
 
                
else
                
{
                    NewURL 
= Regex.Replace(NewURL, OrignalSet, NewSet, RegexOptions.IgnoreCase);
                }

            }
 
            
else if (newQueryStringValue.Trim() != "")
            

                
if (httpRequest.QueryString.Count == 0)
                
{
                    NewURL 
+= string.Format("?{0}={1}", queryStringKey, newQueryStringValue);
                }
 
                
else
                
{
                    NewURL 
+= string.Format("&{0}={1}", queryStringKey, newQueryStringValue);
                }

            }

            
return NewURL;
        }


        
/**//// <summary>
        
/// Number?
        
/// </summary>
        
/// <param name="Text"></param>
        
/// <returns></returns>

        public static bool IsNumeric(string Text)
        
{
            
if (Text == "" || Text == null)
            
{
                
return false;
            }

            Text 
= Text.Trim();
            
bool bResult = Regex.IsMatch(Text, @"^\d+$");
            
return bResult;
        }

        
#endregion


        
build numbers and BACK/NEXT section#region build numbers and BACK/NEXT section

        
protected virtual string BuildPageNumbers(int currentPage, int pageCount)
        
{
            StringBuilder Pages 
= new StringBuilder();

            
int StartPage = 1;

            
//End Page
            int EndPage = 0;
            
if (pageCount > maxPages)
            
{
                EndPage 
= maxPages;
            }

            
else
            
{
                EndPage 
= pageCount;
            }


            
//Adjust for slider 
            int Half = (int)Math.Floor((double)(SliderSize - 1/ 2);
            
//8: Half = 3
            
//7: Half = 3

            
int NumAbove = currentPage + Half + ((SliderSize - 1% 2);
            
int NumBelow = currentPage - Half;

            
if (NumBelow < 1)
            
{
                NumAbove 
+= (1 - NumBelow);
                NumBelow 
= 1;
            }


            
if (NumAbove > EndPage)
            
{
                NumBelow 
-= (NumAbove - EndPage);
                
if (NumBelow < 1)
                
{
                    NumBelow 
= 1;
                }

                NumAbove 
= EndPage;
            }


            StartPage 
= NumBelow;
            EndPage 
= NumAbove;

            
for (int i = StartPage; i <= EndPage; i++)
            
{
                
int RecordStart;
                
int RecordEnd;

                
//For Display
                if (Context == null)
                
{
                    RecordStart 
= ((i - 1* PageSize) + 1;
                    RecordEnd 
= i * PageSize;

                    
if (i > StartPage)
                    
{
                        
if (PageNumbersSeparator.ToLower() == "&nbsp;")
                        
{
                            Pages.AppendFormat(
"{0}", PageNumbersSeparator);
                        }

                        
else
                        
{
                            Pages.AppendFormat(
" {0} ", PageNumbersSeparator);
                        }

                    }


                    
if (i == StartPage)
                    
{
                        Pages.AppendFormat(
"<B>{0}</B>", i.ToString());
                    }

                    
else
                    
{
                        Pages.AppendFormat(
"<a href='#'>{0}</a>", i.ToString());
                    }

                }

                
else
                

                    RecordStart 
= ((i - 1* PageSize) + 1;
 
                    
if (i * PageSize > RecordCount)
                    
{
                        RecordEnd 
= RecordCount;
                    }

                    
else if (RecordCount == PageSize)
                    
{
                        RecordEnd 
= (RecordStart - 1+ PageSize;
                    }

                    
else
                    
{
                        RecordEnd 
= (RecordStart - 1+ RecordCount;
                    }


                    
if (i > StartPage)
                    
{
                        
if (PageNumbersSeparator.ToLower() == "&nbsp;")
                        
{
                            Pages.AppendFormat(
"{0}", PageNumbersSeparator);
                        }

                        
else
                        
{
                            Pages.AppendFormat(
" {0} ", PageNumbersSeparator);
                        }

                    }


                    
if (i == currentPage)
                    
{
                        Pages.AppendFormat(
"<B>{0}</B>", i.ToString());
                    }

                    
else
                    
{
                        Pages.AppendFormat(LINK
                            , i
                            , UpdateQueryStringItem(Context.Request, QueryStringKey, i.ToString()));
                    }

                }

            }

            
return Pages.ToString();
        }


        
protected virtual string BuildLinkNavBack(int currentPage)
        
{
            StringBuilder LinkNav 
= new StringBuilder();

            
//Next Links
            if (currentPage == 1 || RecordCount == 0)
            
{
                
if (ShowFirstLast)
                
{
                    LinkNav.Append(FirstText 
+ "&nbsp;");
                }

                LinkNav.Append(BackText);
            }

            
else
            
{
                
if (ShowFirstLast)
                
{
                    LinkNav.AppendFormat(LINK 
+ "&nbsp;"
                        , FirstText
                        , UpdateQueryStringItem(Context.Request, QueryStringKey, Convert.ToString(
1)));
                }

                LinkNav.AppendFormat(LINK
                    , BackText
                    , UpdateQueryStringItem(Context.Request, QueryStringKey, Convert.ToString(currentPage 
- 1)));
            }


            
return LinkNav.ToString();
        }


        
protected virtual string BuildLinkNavNext(int currentPage, int pageCount)
        
{
            StringBuilder LinkNav 
= new StringBuilder();

            
if (Context == null)
            
{
                LinkNav.AppendFormat(
"<a href='#'>{0}</a>", NextText);
                
if (ShowFirstLast)
                
{
                    LinkNav.AppendFormat(
"&nbsp;<a href='#'>{0}</a>", LastText);
                }

            }

            
else if (currentPage >= maxPages || currentPage == PageCount)
            
{
                LinkNav.Append(NextText);
                
if (ShowFirstLast)
                
{
                    LinkNav.Append(
"&nbsp;" + LastText);
                }

            }

            
else
            
{
                LinkNav.AppendFormat(
"<a href='{0}'>{1}</A>"
                    , UpdateQueryStringItem(Context.Request, QueryStringKey, Convert.ToString(currentPage 
+ 1))
                    , NextText);
                
if (ShowFirstLast)
                
{
                    LinkNav.AppendFormat(
"&nbsp;<a href='{0}'>{1}</A>"
                        , UpdateQueryStringItem(Context.Request, QueryStringKey, Convert.ToString(pageCount))
                        , LastText);
                }

            }


            
return LinkNav.ToString();
        }


        
#endregion


        
Render method#region Render method 

        
protected override void OnPreRender(EventArgs e)
        
{
            maxPages 
= RecordCount / PageSize;
            
base.OnPreRender(e);
        }


        
protected override void Render(HtmlTextWriter writer)
        
{
            StringBuilder sb 
= new StringBuilder();

            sb.Append(BuildPageNumbers(PageIndex, PageCount));
            sb.Append(NumbersSectionSeparator);
            sb.Append(BuildLinkNavBack(PageIndex));
            sb.Append(BackNextSeparator);
            sb.Append(BuildLinkNavNext(PageIndex, PageCount));
            writer.Write(
string.Format(PAGER, sb.ToString(), PagerCssClass));
            
base.Render(writer);
        }


        
#endregion

    }



调用方法
<%@ Register TagPrefix="Repeater" Namespace="Freshers.Component" Assembly="Freshers" %>

<Repeater:Pager ID="Pager" runat="server" SliderSize="5" BackText="上一页" NextText="下一页"
                        FirstText
="首页" LastText="尾页"  PageSize="10" RecordCount="100"/>

转载于:https://www.cnblogs.com/yans/archive/2008/07/03/1234564.html

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值