Export (0) Print
Expand All

DetailsView.CurrentMode Property

Note: This property is new in the .NET Framework version 2.0.

Gets the current data-entry mode of the DetailsView control.

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

public DetailsViewMode CurrentMode { get; }
/** @property */
public DetailsViewMode get_CurrentMode ()

public function get CurrentMode () : DetailsViewMode

Property Value

One of the DetailsViewMode values.

Use the CurrentMode property to determine whether the DetailsView control is in edit, insert, or read-only mode. The following table lists the different mode values.

Mode

Description

DetailsViewMode.Edit

The DetailsView control is in edit mode, which allows the user to update the values of a record.

DetailsViewMode.Insert

The DetailsView control is in insert mode, which allows the user to add a new record to the data source.

DetailsView.ReadOnly

The DetailsView control is in read-only mode, which is the normal display mode.

This value is normally set automatically by the DetailsView control by when the New, Update, Insert, Delete, or Cancel command button is clicked. You can also programmatically change the mode by using the ChangeMode method.

When the DetailsView control changes modes, the events in the following table are raised. This allows you to create a custom event handler that performs a routine when the event occurs.

Event

Description

ModeChanged

Occurs when a command button that changes the mode of the DetailsView control is clicked, but after the mode changes.

ModeChanging

Occurs when a command button that changes the mode of the DetailsView control is clicked, but before the mode changes.

The following code example demonstrates how to use the CurrentMode property to determine whether the DetailsView control is in edit, insert, or read-only mode. If the user attempts to navigate to a different record while the DetailsView control is in edit mode, the paging operation is canceled.


<%@ Page Language="C#" %>

<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>
<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 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft