Edit

Share via


DetailsView.ModeChanging Event

Definition

Occurs when a DetailsView control attempts to change between edit, insert, and read-only mode, but before the CurrentMode property is updated.

public:
 event System::Web::UI::WebControls::DetailsViewModeEventHandler ^ ModeChanging;
public event System.Web.UI.WebControls.DetailsViewModeEventHandler ModeChanging;
member this.ModeChanging : System.Web.UI.WebControls.DetailsViewModeEventHandler 
Public Custom Event ModeChanging As DetailsViewModeEventHandler 

Event Type

Examples

The following code example demonstrates how to use the ModeChanging event to disable the paging feature when the DetailsView control is in edit mode.


<%@ 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_ModeChanging(Object sender, DetailsViewModeEventArgs e)
  {
    // Disable the paging feature in edit mode.
    if (e.NewMode == DetailsViewMode.Edit)
    {
        CustomerDetailView.AllowPaging = false;
    }
    else
    {
        CustomerDetailView.AllowPaging = true;
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView ModeChanging Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView ModeChanging Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneraterows="true"
          autogenerateeditbutton="true" 
          allowpaging="true"
          onmodechanging="CustomerDetailView_ModeChanging" 
          runat="server">
               
          <fieldheaderstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <!-- 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_ModeChanging(ByVal sender As Object, ByVal e As DetailsViewModeEventArgs)
        ' Disable the paging feature in edit mode.
        If e.NewMode = DetailsViewMode.Edit Then
            CustomerDetailView.AllowPaging = False
        Else
            CustomerDetailView.AllowPaging = True
        End If
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView ModeChanging Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView ModeChanging Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneraterows="true"
          autogenerateeditbutton="true" 
          allowpaging="true"
          onmodechanging="CustomerDetailView_ModeChanging" 
          runat="server">
               
          <fieldheaderstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <!-- 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>

Remarks

The ModeChanging event is raised when a DetailsView control attempts to change between edit, insert, and read-only mode, but before the CurrentMode property is updated. This allows you to provide an event handler that performs a custom routine, such as canceling the mode change, whenever this event occurs.

A DetailsViewModeEventArgs object is passed to the event handler, which allows you to determine the new mode, to determine whether the mode change was a result of the user canceling an edit operation, or to cancel the mode change. To determine the new mode, use the NewMode property. To determine whether the mode change was a result of the user canceling an edit operation, use the CancelingEdit property. You can cancel the mode change by setting the Cancel property to true.

For more information about how to handle events, see Handling and Raising Events.

Applies to

See also