Export (0) Print
Expand All

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)

'Declaration
Public Event PageIndexChanging As DetailsViewPageEventHandler
'Usage
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.

NoteNote:

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

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

Community Additions

ADD
Show:
© 2014 Microsoft