GridView.PageIndexChanging 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
發生於按一下其中一個頁面巡覽區按鈕時,但是在 GridView 控制項處理分頁作業之前。
public:
event System::Web::UI::WebControls::GridViewPageEventHandler ^ PageIndexChanging;
public event System.Web.UI.WebControls.GridViewPageEventHandler PageIndexChanging;
member this.PageIndexChanging : System.Web.UI.WebControls.GridViewPageEventHandler
Public Custom Event PageIndexChanging As GridViewPageEventHandler
事件類型
範例
下列範例示範如果使用者嘗試在控制項處於編輯模式時 GridView 流覽至另一個頁面,則如何使用 PageIndexChanging 事件來取消分頁作業。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomersGridView_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
// Cancel the paging operation if the user attempts to navigate
// to another page while the GridView control is in edit mode.
if (CustomersGridView.EditIndex != -1)
{
// Use the Cancel property to cancel the paging operation.
e.Cancel = true;
// Display an error message.
int newPageNumber = e.NewPageIndex + 1;
Message.Text = "Please update the record before moving to page " +
newPageNumber.ToString() + ".";
}
else
{
// Clear the error message.
Message.Text = "";
}
}
void CustomersGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
{
// Clear the error message.
Message.Text = "";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView PageIndexChanging Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView PageIndexChanging Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
allowpaging="true"
autogenerateeditbutton="true"
datakeynames="CustomerID"
onpageindexchanging="CustomersGridView_PageIndexChanging"
onrowcancelingedit="CustomersGridView_RowCancelingEdit"
runat="server">
<pagersettings mode="Numeric"
position="Bottom"
pagebuttoncount="10"/>
<pagerstyle backcolor="LightBlue"/>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomersGridView_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
' Cancel the paging operation if the user attempts to navigate
' to another page while the GridView control is in edit mode.
If CustomersGridView.EditIndex <> -1 Then
' Use the Cancel property to cancel the paging operation.
e.Cancel = True
' Display an error message.
Dim newPageNumber As Integer = e.NewPageIndex + 1
Message.Text = "Please update the record before moving to page " & _
newPageNumber.ToString() & "."
Else
' Clear the error message.
Message.Text = ""
End If
End Sub
Sub CustomersGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
' Clear the error message.
Message.Text = ""
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView PageIndexChanging Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView PageIndexChanging Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
allowpaging="true"
autogenerateeditbutton="true"
datakeynames="CustomerID"
onpageindexchanging="CustomersGridView_PageIndexChanging"
onrowcancelingedit="CustomersGridView_RowCancelingEdit"
runat="server">
<pagersettings mode="Numeric"
position="Bottom"
pagebuttoncount="10"/>
<pagerstyle backcolor="LightBlue"/>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
備註
PageIndexChanging按一下其中一個呼叫器按鈕,但在控制項處理分頁作業之前 GridView ,就會引發 事件。 這可讓您提供事件處理方法,以執行自訂常式,例如每當發生此事件時取消分頁作業。
注意
當您以程式設計方式設定 PageIndex 屬性時,不會引發此事件。
GridViewPageEventArgs物件會傳遞至事件處理方法,可讓您判斷使用者所選取頁面的索引,並指出應該取消分頁作業。 若要取消分頁作業,請將 Cancel 物件的 屬性 GridViewPageEventArgs 設定為 true
。
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。