共用方式為


DetailsView.CurrentMode 屬性

定義

取得 DetailsView 控制項目前的資料輸入模式。

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; }
[<System.ComponentModel.Browsable(false)>]
member this.CurrentMode : System.Web.UI.WebControls.DetailsViewMode
Public ReadOnly Property CurrentMode As DetailsViewMode

屬性值

其中一個 DetailsViewMode 值。

屬性

範例

下列程式碼範例示範如何使用 CurrentMode 屬性來判斷 DetailsView 控制項是否處於編輯、插入或唯讀模式。 如果使用者嘗試在控制項處於編輯模式時 DetailsView 流覽至不同的記錄,則會取消分頁作業。


<%@ 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>

備註

CurrentMode使用 屬性來判斷 DetailsView 控制項是否處於編輯、插入或唯讀模式。 下表列出不同的模式值。

[模式] 描述
DetailsViewMode.Edit 控制項 DetailsView 處於編輯模式,可讓使用者更新記錄的值。
DetailsViewMode.Insert 控制項 DetailsView 處於插入模式,可讓使用者將新記錄新增至資料來源。
DetailsViewMode.ReadOnly 控制項 DetailsView 處於唯讀模式,這是一般顯示模式。

按一下 [新增]、[更新]、[插入]、[刪除] 或 [取消] 命令按鈕時,控制項通常會自動 DetailsView 設定此值。 您也可以使用 ChangeMode 方法,以程式設計方式變更模式。

DetailsView當控制項變更模式時,會引發下表中的事件。 這可讓您建立自訂事件處理常式,以在事件發生時執行常式。

事件 描述
ModeChanged 發生于按一下變更控制項模式的 DetailsView 命令按鈕時,但在模式變更之後。
ModeChanging 發生于按一下變更控制項模式的 DetailsView 命令按鈕時,但在模式變更之前。

適用於

另請參閱