PostBack方式分页时,当页导航元素之一被单击或用户手工输入页索引提交时发生,Url方式分页时,页面第一次加载时发生。
C# 语法:
public event PageChangingEventHandler PageChanging;
备注:
该事件在 AspNetPager 处理分页操作前引发,因此可以在事件处理程序中根据需要取消分页操作。
示例:
下面的示例显示如何为PageChanging事件指定事件处理程序,当用户选中页面上的“取消分页”复选框时,取消分页操作:
<%@ 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事件被引发,新页面的索引是:" + e.NewPageIndex);
if(CheckBox1.Checked){
e.Cancel=true;
Response.Write("<br/>分页操作被取消");
}
}
void AspNetPager1_PageChanged(object src, EventArgs e)
{
bindData();
Response.Write("<br/>PageChanged事件被引发,当前页索引是:"+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="取消分页" />
<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>