DetailsView.PageIndexChanging Evento
Definição
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
Tipo de evento
Exemplos
O exemplo de código a seguir demonstra como usar o PageIndexChanging evento para cancelar uma operação de paginação.The following code example demonstrates how to use the PageIndexChanging event to cancel a paging operation.
<%@ 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>
Comentários
O DetailsView controle gera o PageIndexChanging evento quando um botão de pager (um botão com sua CommandName propriedade definido como "Page") dentro do controle é clicado, mas antes de o DetailsView controle manipular a operação de paginação.The DetailsView control raises the PageIndexChanging event when a pager button (a button with its CommandName property set to "Page") within the control is clicked, but before the DetailsView control handles the paging operation. Isso permite que você forneça um manipulador de eventos que executa uma rotina personalizada, como cancelar a operação de paginação, sempre que esse evento ocorrer.This allows you to provide an event handler that performs a custom routine, such as canceling the paging operation, whenever this event occurs.
Observação
Esse evento não é gerado quando você define a propriedade de forma programática PageIndex .This event is not raised when you programmatically set the PageIndex property.
Os botões de pager geralmente estão localizados na linha do pager de um DetailsView controle.Pager buttons are usually located in the pager row of a DetailsView control.
Um DetailsViewPageEventArgs objeto é passado para o manipulador de eventos, que permite determinar o índice da página selecionada pelo usuário e indicar que a operação de paginação deve ser cancelada.A DetailsViewPageEventArgs object is passed to the event handler, which allows you to determine the index of the page selected by the user and to indicate that the paging operation should be canceled. Para determinar o índice da página selecionada pelo usuário, use a NewPageIndex propriedade.To determine the index of the page selected by the user, use the NewPageIndex property. Para cancelar a operação de paginação, defina a Cancel Propriedade do DetailsViewPageEventArgs objeto como true .To cancel the paging operation, set the Cancel property of the DetailsViewPageEventArgs object to true.
Para obter mais informações sobre como lidar com eventos, consulte manipulando e gerando eventos.For more information about how to handle events, see Handling and Raising Events.