共用方式為


DetailsView.ModeChanged 事件

定義

發生於 DetailsView 控制項嘗試在編輯、插入和唯讀模式之間變更,但是在 CurrentMode 屬性更新之後。

public:
 event EventHandler ^ ModeChanged;
public event EventHandler ModeChanged;
member this.ModeChanged : EventHandler 
Public Custom Event ModeChanged As EventHandler 

事件類型

範例

下列程式碼範例示範如何使用 ModeChanged 事件,在標頭資料列中顯示目前的模式。


<%@ 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_ModeChanged(Object sender, EventArgs e)
  {
    // Display the current mode in the header row.
      switch (CustomerDetailView.CurrentMode)
    {
      case DetailsViewMode.Edit:
        CustomerDetailView.HeaderText = "Edit Mode";
        CustomerDetailView.HeaderStyle.ForeColor = System.Drawing.Color.Red;
        CustomerDetailView.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
        break;
      case DetailsViewMode.Insert:
        CustomerDetailView.HeaderText = "Insert Mode";
        CustomerDetailView.HeaderStyle.ForeColor = System.Drawing.Color.Green;
        CustomerDetailView.HeaderStyle.BackColor = System.Drawing.Color.Yellow;
        break;
      case DetailsViewMode.ReadOnly:
        CustomerDetailView.HeaderText = "Read-Only Mode";
        CustomerDetailView.HeaderStyle.ForeColor = System.Drawing.Color.Blue;
        CustomerDetailView.HeaderStyle.BackColor = System.Drawing.Color.White;
        break;
      default:
          CustomerDetailView.HeaderText = "Error!";
        break;
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView ModeChanged Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView ModeChanged Example</h3>      
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneraterows="true"
          autogenerateeditbutton="true"
          autogenerateinsertbutton="true"  
          allowpaging="true"
          headertext="Read-Only Mode" 
          onmodechanged="CustomerDetailView_ModeChanged" 
          runat="server">
               
          <fieldheaderstyle backcolor="Navy"
            forecolor="White"/>
            
          <headerstyle forecolor="Blue"/>
                    
        </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_ModeChanged(ByVal sender As Object, ByVal e As EventArgs)
  
        ' Display the current mode in the header row.
        Select Case CustomerDetailView.CurrentMode

            Case DetailsViewMode.Edit
                CustomerDetailView.HeaderText = "Edit Mode"
                CustomerDetailView.HeaderStyle.ForeColor = System.Drawing.Color.Red
                CustomerDetailView.HeaderStyle.BackColor = System.Drawing.Color.LightGray

            Case DetailsViewMode.Insert
                CustomerDetailView.HeaderText = "Insert Mode"
                CustomerDetailView.HeaderStyle.ForeColor = System.Drawing.Color.Green
                CustomerDetailView.HeaderStyle.BackColor = System.Drawing.Color.Yellow

            Case DetailsViewMode.ReadOnly
                CustomerDetailView.HeaderText = "Read-Only Mode"
                CustomerDetailView.HeaderStyle.ForeColor = System.Drawing.Color.Blue
                CustomerDetailView.HeaderStyle.BackColor = System.Drawing.Color.White

            Case Else
                CustomerDetailView.HeaderText = "Error!"
        
        End Select
    
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView ModeChanged Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView ModeChanged Example</h3>      
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneraterows="true"
          autogenerateeditbutton="true"
          autogenerateinsertbutton="true"  
          allowpaging="true"
          headertext="Read-Only Mode" 
          onmodechanged="CustomerDetailView_ModeChanged" 
          runat="server">
               
          <fieldheaderstyle backcolor="Navy"
            forecolor="White"/>
            
          <headerstyle forecolor="Blue"/>
                    
        </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>

備註

ModeChangedDetailsView 控制項嘗試在編輯、插入和唯讀模式之間變更,但在屬性更新之後 CurrentMode ,就會引發 事件。 這可讓您提供事件處理常式來執行自訂常式,例如每當發生此事件時,將控制項與另一個控制項同步 DetailsView 處理。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱