DetailsView.CurrentMode 属性

定义

获取 DetailsView 控件的当前数据输入模式。Gets the current data-entry mode of the DetailsView control.

public:
 property System::Web::UI::WebControls::DetailsViewMode CurrentMode { System::Web::UI::WebControls::DetailsViewMode get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.WebControls.DetailsViewMode CurrentMode { get; }
member this.CurrentMode : System.Web.UI.WebControls.DetailsViewMode
Public ReadOnly Property CurrentMode As DetailsViewMode

属性值

DetailsViewMode 值之一。One of the DetailsViewMode values.

属性

示例

下面的代码示例演示如何使用CurrentMode属性来确定DetailsView控件是处于编辑、插入还是只读模式。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. 如果用户在DetailsView控件处于编辑模式时尝试导航到另一条记录, 则会取消分页操作。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#" %>

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

注解

使用属性可确定DetailsView控件是处于编辑、插入还是只读模式。 CurrentModeUse 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 DetailsView控件处于编辑模式, 这允许用户更新记录的值。The DetailsView control is in edit mode, which allows the user to update the values of a record.
DetailsViewMode.Insert DetailsView控件处于插入模式, 这允许用户向数据源添加新记录。The DetailsView control is in insert mode, which allows the user to add a new record to the data source.
DetailsViewMode.ReadOnly DetailsView控件处于只读模式, 这是正常的显示模式。The DetailsView control is in read-only mode, which is the normal display mode.

单击 "新建"、"更新" DetailsView 、"插入"、"删除" 或 "取消" 命令按钮时, 控件通常会自动设置此值。This value is normally set automatically by the DetailsView control by when the New, Update, Insert, Delete, or Cancel command button is clicked. 你还可以使用ChangeMode方法以编程方式更改模式。You can also programmatically change the mode by using the ChangeMode method.

DetailsView当控件更改模式时, 将引发下表中的事件。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 当单击更改DetailsView控件模式的命令按钮时, 但在模式更改之后发生。Occurs when a command button that changes the mode of the DetailsView control is clicked, but after the mode changes.
ModeChanging 当单击更改DetailsView控件模式的命令按钮时, 但在模式更改之前发生。Occurs when a command button that changes the mode of the DetailsView control is clicked, but before the mode changes.

适用于

另请参阅