DetailsView.ModeChanging 事件

定义

DetailsView 控件尝试在编辑、插入和只读模式之间更改时,但在更新 CurrentMode 属性之前发生。

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 

事件类型

示例

下面的代码示例演示如何在控件处于编辑模式时DetailsView使用 ModeChanging 事件禁用分页功能。


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

注解

ModeChangingDetailsView控件尝试在编辑、插入和只读模式之间更改时,但在更新 属性之前,将CurrentMode引发 该事件。 这允许你提供一个事件处理程序,用于在发生此事件时执行自定义例程,例如取消模式更改。

对象 DetailsViewModeEventArgs 将传递给事件处理程序,该处理程序允许你确定新模式、确定模式更改是用户取消编辑操作的结果,还是取消模式更改。 若要确定新模式,请使用 NewMode 属性。 若要确定模式更改是否是用户取消编辑操作的结果,请使用 CancelingEdit 属性。 可以通过将 属性设置为 Canceltrue来取消模式更改。

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于

另请参阅