Fork me on GitHub

PageChanging Event

Occurs when one of the pager buttons is clicked, but before the AspNetPager control handles the paging operation.

C# Syntax:

public event PageChangingEventHandler PageChanging;

Example:

The following example demonstrates how to cancel paging event where "cancel paging" checkbox is checked:
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<%@Import NameSpace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<script runat="server" Language="C#">
void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int totalOrders = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "P_GetOrderNumber");
            AspNetPager1.RecordCount = totalOrders;
            bindData();
        }
    }
 void bindData()
    {
        Repeater1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"P_GetPagedOrders2005",
            new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
            new SqlParameter("@pageSize", AspNetPager1.PageSize));
        Repeater1.DataBind();
    }
void AspNetPager1_PageChanging(object src,PageChangingEventArgs e){
 Response.Write("PageChanging event fired, new page index is " + e.NewPageIndex);
if(CheckBox1.Checked){
	e.Cancel=true;
    Response.Write("<br/>paging operation is canceled");
 }
}
 void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        bindData();
        Response.Write("<br/>PageChanged is fired, the new page index is "+AspNetPager1.CurrentPageIndex);
    }
</script>
<HTML><HEAD>
<TITLE> AspNetPager Samples </TITLE>
<META NAME="Author" CONTENT="Webdiyer (www.webdiyer.com)">
</HEAD>
<BODY>
<form runat="server">
    <webdiyer:aspnetpager id="AspNetPager1" runat="server" OnPageChanging="AspNetPager1_PageChanging" onpagechanged="AspNetPager1_PageChanged" 
    Width="100%" HorizontalAlign="center"></webdiyer:aspnetpager>
    <asp:CheckBox ID="CheckBox1" runat="server" Text="cancel paging" />
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
        <table width="100%" border="1" cellspacing="0" cellpadding="4" style="border-collapse:collapse">
        <tr style="backGround-color:#CCCCFF"><th style="width:15%"><asp:Literal ID="Literal1" runat="server" Text="<%$Resources:LocalizedText,orderid%>" /></th><th style="width:15%"><asp:Literal ID="Literal3" runat="server" Text="<%$Resources:LocalizedText,orderdate%>" /></th><th style="width:30%"><asp:Literal ID="Literal5" runat="server" Text="<%$Resources:LocalizedText,companyname%>" /></th><th style="width:20%"><asp:Literal ID="Literal2" runat="server" Text="<%$Resources:LocalizedText,customerid%>" /></th><th style="width:20%"><asp:Literal ID="Literal4" runat="server" Text="<%$Resources:LocalizedText,employeename%>" /></th></tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr style="background-color:#FAF3DC">
        <td><%#DataBinder.Eval(Container.DataItem,"orderid")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"orderdate","{0:d}")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "companyname")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"customerid")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"employeename")%></td>
        </tr>
        </ItemTemplate>
		<FooterTemplate>
        </table>
        </FooterTemplate>
        </asp:Repeater>
        <webdiyer:AspNetPager ID="AspNetPager2" runat="server" CloneFrom="AspNetPager1">
        </webdiyer:AspNetPager>
		</form>
</BODY>
</HTML>