原创asp.net2分页控件

来源:互联网 发布:重庆 网络推广方案 编辑:程序博客网 时间:2024/05/25 12:21

惭愧,又失业了.......

自己做了个邮件系统,运用了webpart,ajax,FrontControl,Hibernate等技术,也算检验这段时间漫长的学习过程.

网上流行AspnetPager分页控件,我找不到最新版源码,所以自己动手写了一个.分布在blog上,或许也有人和我一样想法自己想做分页控件的,可以参考交流一下.

分页控件相当简易,甚至有点简陋,但跟HQL和GridView一起工作得相当好,而且代码很容易维护,扩展功能也很方便.

3个文件:

  • EventArgs.cs 置于App_Code下.
  • PageIndicator.ascx  控件
  • PageIndicator.ascx.cs  代码文件

EventArgs.cs

//2006年heguosohu@sohu.com原创
//http://blog.csdn.net/heguo

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

//定义一个泛型的EventArgs
public class EventArgs<T> : EventArgs
{
    
private T value;

    
public EventArgs(T value)
    
{
        
this.value = value;
    }


    
public T Value
    
{
        
get return value; }
    }

}


 PageIndicator.ascx 

如果不用Ajax,需要去掉UpdatePanel相关的元

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PageIndicator.ascx.cs" Inherits="PageIndicator" %>
    
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        
<ContentTemplate>
            
<span
                            
style="font-size: 9pt">
                
<div style="width: 609px; height: 32px">
                  
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" Font-Bold="False"
            Font-Italic
="False" Font-Names="Webdings" Font-Size="11pt" CausesValidation="False" Font-Underline="False">9</asp:LinkButton>&nbsp;
        
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click" CausesValidation="False" Font-Bold="False" Font-Italic="False" Font-Names="Webdings" Font-Size="11pt" Font-Underline="False">7</asp:LinkButton>&nbsp;
    
    
<asp:Literal ID="Literal1" runat="server" Text="Literal"></asp:Literal>
                    
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                    
&nbsp; <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click" CausesValidation="False" Font-Bold="False" Font-Italic="False" Font-Names="Webdings" Font-Size="11pt" Font-Underline="False">8</asp:LinkButton>
            
<asp:LinkButton ID="LinkButton4" runat="server" OnClick="LinkButton4_Click" Font-Bold="False"
                Font-Names
="Webdings" Font-Size="11pt" CausesValidation="False" Font-Italic="False" Font-Underline="False">:</asp:LinkButton><span style="font-size: 9pt">
                    
&nbsp;&nbsp;
                    
<asp:Literal ID="Literal2" runat="server" Text="Literal"></asp:Literal>
                
</span><span style="font-size: 9pt">&nbsp;&nbsp;
                    
<asp:LinkButton ID="LinkButton5" runat="server" OnClick="LinkButton5_Click" CausesValidation="False">转到</asp:LinkButton></span><asp:TextBox
                        
ID="TextBox1" runat="server" BorderStyle="Groove" Width="27px" style="text-align:center;height:12px;width:20px;"></asp:TextBox><span
                            
style="font-size: 9pt"><asp:RegularExpressionValidator ID="RegularExpressionValidator1"
                                runat
="server" ControlToValidate="TextBox1" ErrorMessage="请输入数字" ValidationExpression="d*"></asp:RegularExpressionValidator>
                            
</span></span></div>

                
</div>
            
</span>
        
</ContentTemplate>
    
</asp:UpdatePanel>
      

 

 PageIndicator.ascx.cs 

 

//2006年heguosohu@sohu.com原创
//http://blog.csdn.net/heguo

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 PageIndicator : System.Web.UI.UserControl
{
    
private static readonly string itemCountKey = "ItemCount";
    
private static readonly string currentPageIndex = "CurrentPageIndex";
    
private static readonly string pageSize = "PageSize";
    
private static readonly string indicatorCount = "IndicatorCount";
    
private static readonly string tipFormat = "共{0}页{1}封邮件";
    
private static readonly string createdFrom = "CreatedFrom";
    
private static readonly string createdEnd = "CreatedEnd";

    
public int IndicatorFrom
    
{
        
get
        
{
            
return CurrentPageIndex - (CurrentPageIndex - 1% IndicatorCount;
        }

    }


    
public int IndicatorCount
    
{
        
get return ViewState[indicatorCount] == null ? 5 : (int)ViewState[indicatorCount]; }
        
set { ViewState[indicatorCount] = value; }
    }



    
public int ItemCount
    
{
        
get return ViewState[itemCountKey] == null ? 0 : (int)ViewState[itemCountKey]; }
    }


    
public int PageSize
    
{
        
get return ViewState[pageSize] == null ? 10 : (int)ViewState[pageSize]; }
        
set
        
{
            ViewState[pageSize] 
= value > 0 ? value : 10;
        }

    }


    
public int PageCount
    
{
        
get return ItemCount == 0?1: (ItemCount - 1/ PageSize + 1; }
    }


    
public int CurrentPageFirstItemIndex
    
{
        
get return CurrentPageIndex * PageSize + 1; }
    }


    
protected void LinkButton1_Click(object sender, EventArgs e)
    
{
        
this.SetPageIndex(1);
    }

    
protected void LinkButton2_Click(object sender, EventArgs e)
    
{
       
this.SetPageIndex(CurrentPageIndex - 1);
    }

    
protected void LinkButton3_Click(object sender, EventArgs e)
    
{
        
this.SetPageIndex(CurrentPageIndex + 1);
    }

    
protected void LinkButton4_Click(object sender, EventArgs e)
    
{
        
this.SetPageIndex(PageCount);
    }

    
protected void LinkButton5_Click(object sender, EventArgs e)
    
{
        
this.RegularExpressionValidator1.Validate();
        
if (RegularExpressionValidator1.IsValid)
            
this.SetPageIndex(int.Parse(TextBox1.Text));
        
else
            
this.SetPageIndex(1);
    }


    
void Page_Init(object sender, EventArgs e)
    
{
        
    }



    
public int CurrentPageIndex
    
{
        
        
get return ViewState[currentPageIndex] == null ? 1 : (int)ViewState[currentPageIndex]; }
    }


    
public void SetPageIndex(int pageIndex)
    
{
        
if (pageIndex < 1) pageIndex = 1;
        
if (pageIndex > PageCount) pageIndex = PageCount;
        ViewState[currentPageIndex] 
= pageIndex;
        UpdatePanel1.Update();
        
if (PageIndicatorChanged != null)
            PageIndicatorChanged(
thisnew EventArgs<int>(pageIndex));
    }


    
protected void Page_Load(object sender, EventArgs e)
    
{
        CreateLinkButtons(CreatedFrom, CreatedEnd);
    }


    
protected void Page_PreRender(object sender, EventArgs e)
    
{
        Literal2.Text 
= string.Format(tipFormat, PageCount, ItemCount);
        TextBox1.Text 
= CurrentPageIndex.ToString();
        Literal1.Text 
= string.Empty;
        CreateLinkButtons(IndicatorFrom, Math.Min(IndicatorFrom 
+ IndicatorCount, PageCount + 1));
    }


    
void CreateLinkButtons(int from,int end)
    
{
        
this.PlaceHolder1.Controls.Clear();
        
for (int i = from; i < end; i++)
        
{
            LinkButton linkbutton 
= new LinkButton();
            linkbutton.ID 
= "_link" + i.ToString();
            linkbutton.Text 
= i == CurrentPageIndex ? i.ToString() : "[" + i.ToString()+"]";
            linkbutton.CausesValidation 
= false;
            linkbutton.Click 
+= new EventHandler(linkbutton_Click);
            
this.PlaceHolder1.Controls.Add(linkbutton);
            Label l 
= new Label();
            l.Text 
= " ";
            l.ID 
= "_label" + i.ToString();
            
this.PlaceHolder1.Controls.Add(l);
        }

        CreatedFrom 
= from;
        CreatedEnd 
= end;
    }


    
int CreatedFrom
    
{
        
get return ViewState[createdFrom] == null ? 0 : (int)ViewState[createdFrom]; }
        
set { ViewState[createdFrom] = value; }
    }

    
int CreatedEnd
    
{
        
get return ViewState[createdEnd] == null ? 0 : (int)ViewState[createdEnd]; }
        
set { ViewState[createdEnd] = value; }
    }

    
void linkbutton_Click(object sender, EventArgs e)
    
{
        LinkButton l 
= (LinkButton)sender;
        SetPageIndex(
int.Parse(l.ID.Substring(5)));
    }


   
//点击页号抛出的事件
    public event EventHandler<EventArgs<int>> PageIndicatorChanged;
   
   
//检索数据前,需要知道记录总数
    public void Reset(int itemCount)
    
{
        ViewState[itemCountKey] 
= itemCount; 
        
this.SetPageIndex(1);
    }

}



原创粉丝点击