DetailsView.PageIndexChanging 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当 PageIndex 属性的值在分页操作前更改时发生。
public:
event System::Web::UI::WebControls::DetailsViewPageEventHandler ^ PageIndexChanging;
public event System.Web.UI.WebControls.DetailsViewPageEventHandler PageIndexChanging;
member this.PageIndexChanging : System.Web.UI.WebControls.DetailsViewPageEventHandler
Public Custom Event PageIndexChanging As DetailsViewPageEventHandler
事件类型
示例
下面的代码示例演示如何使用 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 CustomerDetailView_ItemCommand(Object sender,
DetailsViewCommandEventArgs e)
{
// Clear the error message if the user cancels the edit
// operation.
if (e.CommandName == "Cancel")
{
ErrorMessageLabel.Text = "";
}
}
protected void CustomerDetailView_PageIndexChanging(
object sender, DetailsViewPageEventArgs e)
{
// Cancel the paging operation if the user tries to
// navigate to another record while in edit mode.
if (CustomerDetailView.CurrentMode == DetailsViewMode.Edit)
{
e.Cancel = true;
// Display an error message.
ErrorMessageLabel.Text =
"You cannot navigate to another record while in edit mode.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
DetailsView CurrentMode Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>
DetailsView CurrentMode Example</h3>
<asp:DetailsView ID="CustomerDetailView"
DataSourceID="DetailsViewSource"
AutoGenerateRows="true"
AutoGenerateEditButton="true"
DataKeyNames="CustomerID"
GridLines="Both"
AllowPaging="true"
OnItemCommand="CustomerDetailView_ItemCommand"
runat="server"
OnPageIndexChanging="CustomerDetailView_PageIndexChanging">
<HeaderStyle BackColor="Navy" ForeColor="White" />
</asp:DetailsView>
<br />
<asp:Label ID="ErrorMessageLabel"
ForeColor="Red" runat="server" />
<!-- 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="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</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 CustomerDetailView_ItemCommand(ByVal sender As Object, _
ByVal e As DetailsViewCommandEventArgs)
' Clear the error message if the user cancels the edit
' operation.
If e.CommandName = "Cancel" Then
ErrorMessageLabel.Text = ""
End If
End Sub
Protected Sub CustomerDetailView_PageIndexChanging( _
ByVal sender As Object, ByVal e As DetailsViewPageEventArgs)
' Cancel the paging operation if the user tries to navigate
' to another record while in edit mode.
If CustomerDetailView.CurrentMode = DetailsViewMode.Edit Then
e.Cancel = True
' Display an error message.
ErrorMessageLabel.Text = _
"You cannot navigate to another record while in edit mode."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
DetailsView CurrentMode Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>
DetailsView CurrentMode Example</h3>
<asp:DetailsView ID="CustomerDetailView"
DataSourceID="DetailsViewSource"
AutoGenerateRows="true"
AutoGenerateEditButton="true"
DataKeyNames="CustomerID"
GridLines="Both"
AllowPaging="true"
OnItemCommand="CustomerDetailView_ItemCommand"
runat="server"
OnPageIndexChanging="CustomerDetailView_PageIndexChanging">
<HeaderStyle BackColor="Navy" ForeColor="White" />
</asp:DetailsView>
<br />
<asp:Label ID="ErrorMessageLabel"
ForeColor="Red" runat="server" />
<!-- 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="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</form>
</body>
</html>
注解
DetailsView当页导航按钮 (其属性设置为“Page”的按钮CommandName
时,控件将引发 PageIndexChanging 事件) 控件内的单击,但在控件DetailsView处理分页操作之前。 这允许你提供一个事件处理程序,用于执行自定义例程,例如,每当发生此事件时取消分页操作。
注意
以编程方式设置 属性时,不会引发此 PageIndex 事件。
寻呼按钮通常位于控件的 DetailsView 页导航行中。
对象 DetailsViewPageEventArgs 传递给事件处理程序,该处理程序允许您确定用户选择的页面的索引,并指示应取消分页操作。 若要确定用户选择的页面的索引,请使用 NewPageIndex 属性。 若要取消分页操作,请将 对象的 属性DetailsViewPageEventArgs设置为 Canceltrue
。
有关如何处理事件的详细信息,请参阅 处理和引发事件。