This documentation is archived and is not being maintained.

DetailsView.PageIndexChanging Event

Occurs when the value of the PageIndex property changes before a paging operation.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

Public Event PageIndexChanging As DetailsViewPageEventHandler
Dim instance As DetailsView 
Dim handler As DetailsViewPageEventHandler 

AddHandler instance.PageIndexChanging, handler
<asp:DetailsView OnPageIndexChanging="DetailsViewPageEventHandler" />

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. This allows you to provide an event handler that performs a custom routine, such as canceling the paging operation, whenever this event occurs.


This event is not raised when you programmatically set the PageIndex property.

Pager buttons are usually located in the pager row of a DetailsView control.

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. To determine the index of the page selected by the user, use the NewPageIndex property. To cancel the paging operation, set the Cancel property of the DetailsViewPageEventArgs object to true.

For more information about handling events, see Consuming Events.

The following code example demonstrates how to use the PageIndexChanging event to cancel a paging operation.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<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

<html xmlns="" >
<head runat="server">
            DetailsView CurrentMode Example</title>
    <form id="Form1" runat="server">
            DetailsView CurrentMode Example</h3>
        <asp:DetailsView ID="CustomerDetailView" 

          <HeaderStyle BackColor="Navy" ForeColor="White" />

        <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" 
            "<%$ 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 

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0