DetailsView.CurrentMode Proprietà

Definizione

Ottiene la modalità di immissione dati corrente del controllo 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

Valore della proprietà

Uno dei valori di DetailsViewMode.

Attributi

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare la CurrentMode proprietà per determinare se il DetailsView controllo è in modalità di modifica, inserimento o sola lettura. Se l'utente tenta di passare a un record diverso mentre il DetailsView controllo è in modalità di modifica, l'operazione di paging viene annullata.


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

Commenti

Utilizzare la CurrentMode proprietà per determinare se il DetailsView controllo è in modalità di modifica, inserimento o sola lettura. Nella tabella seguente sono elencati i diversi valori della modalità.

Mode Descrizione
DetailsViewMode.Edit Il DetailsView controllo è in modalità di modifica, che consente all'utente di aggiornare i valori di un record.
DetailsViewMode.Insert Il DetailsView controllo è in modalità di inserimento, che consente all'utente di aggiungere un nuovo record all'origine dati.
DetailsViewMode.ReadOnly Il DetailsView controllo è in modalità di sola lettura, ovvero la modalità di visualizzazione normale.

Questo valore viene in genere impostato automaticamente dal DetailsView controllo quando si fa clic sul pulsante di comando Nuovo, Aggiorna, Inserisci, Elimina o Annulla. È anche possibile modificare la modalità a livello di codice usando il ChangeMode metodo .

Quando il DetailsView controllo cambia modalità, vengono generati gli eventi nella tabella seguente. In questo modo è possibile creare un gestore eventi personalizzato che esegue una routine quando si verifica l'evento.

Event Descrizione
ModeChanged Si verifica quando si fa clic su un pulsante di comando che modifica la modalità del DetailsView controllo, ma dopo la modifica della modalità.
ModeChanging Si verifica quando si fa clic su un pulsante di comando che modifica la modalità del DetailsView controllo, ma prima che la modalità venga modificata.

Si applica a

Vedi anche