FormView.CurrentMode Proprietà

Definizione

Ottiene la modalità di immissione dati corrente del controllo FormView.

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

Valore della proprietà

FormViewMode

Uno dei valori di FormViewMode.

Attributi

Esempio

Nell'esempio seguente viene illustrato come usare la CurrentMode proprietà per determinare se il FormView controllo è in modalità di modifica, inserimento o sola lettura. Se l'utente tenta di passare a un altro record mentre il FormView controllo è in fase 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 EmployeeFormView_OnPageIndexChanging(Object sender, FormViewPageEventArgs e)
  {
    // Cancel the paging operation if the user attempts to navigate 
    // to another record while the FormView control is in edit mode. 
    if (EmployeeFormView.CurrentMode == FormViewMode.Edit)
    {
      e.Cancel = true;
      MessageLabel.Text = 
        "Please complete the update operation before navigating to another record.";
    }
  }

  void EmployeeFormView_OnModeChanged(Object sender, EventArgs e)
  {
    // Clear the message label.
    MessageLabel.Text = "";
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormView Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormView Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        emptydatatext="No employees found."
        onpageindexchanging="EmployeeFormView_OnPageIndexChanging" 
        onmodechanged="EmployeeFormView_OnModeChanged"  
        runat="server">
        
        <rowstyle backcolor="LightGreen"
          wrap="false"/>
        <editrowstyle backcolor="LightBlue"
          wrap="false"/>

        <itemtemplate>
          <table>
            <tr>
              <td rowspan="4">
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b>Name:</b>
              </td>
              <td>
                <%# Eval("FirstName") %> <%# Eval("LastName") %>
              </td>
            </tr>
            <tr>
              <td>
                <b>Title:</b>
              </td>
              <td>
                <%# Eval("Title") %>
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="Edit"
                  text="Edit"
                  commandname="Edit"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </itemtemplate>
        <edititemtemplate>
          <table>
            <tr>
              <td rowspan="4">
                <asp:image id="EmployeeEditImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b>Name:</b>
              </td>
              <td>
                <asp:textbox id="FirstNameUpdateTextBox"
                  text='<%# Bind("FirstName") %>'
                  runat="server"/>
                <asp:textbox id="LastNameUpdateTextBox"
                  text='<%# Bind("LastName") %>'
                  runat="server"/>
              </td>
            </tr>
            <tr>
              <td>
                <b>Title:</b>
              </td>
              <td>
                <asp:textbox id="TitleUpdateTextBox"
                  text='<%# Bind("Title") %>'
                  runat="server"/> 
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="UpdateButton"
                  text="Update"
                  commandname="Update"
                  runat="server"/>
                <asp:linkbutton id="CancelButton"
                  text="Cancel"
                  commandname="Cancel"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </edititemtemplate> 
                  
      </asp:formview>
      
      <br/><br/>
      
      <asp:Label id="MessageLabel"
        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="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title Where [EmployeeID]=@EmployeeID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </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 EmployeeFormView_OnPageIndexChanging(ByVal sender As Object, ByVal e As FormViewPageEventArgs)

    ' Cancel the paging operation if the user attempts to navigate 
    ' to another record while the FormView control is in edit mode. 
    If EmployeeFormView.CurrentMode = FormViewMode.Edit Then
    
      e.Cancel = True
      MessageLabel.Text = _
        "Please complete the update operation before navigating to another record."
    
    End If
      
  End Sub

  Sub EmployeeFormView_OnModeChanged(ByVal sender As Object, ByVal e As EventArgs)
 
    ' Clear the message label.
    MessageLabel.Text = ""
    
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormView Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormView Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        emptydatatext="No employees found."
        onpageindexchanging="EmployeeFormView_OnPageIndexChanging" 
        onmodechanged="EmployeeFormView_OnModeChanged"  
        runat="server">
        
        <rowstyle backcolor="LightGreen"
          wrap="false"/>
        <editrowstyle backcolor="LightBlue"
          wrap="false"/>

        <itemtemplate>
          <table>
            <tr>
              <td rowspan="4">
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b>Name:</b>
              </td>
              <td>
                <%# Eval("FirstName") %> <%# Eval("LastName") %>
              </td>
            </tr>
            <tr>
              <td>
                <b>Title:</b>
              </td>
              <td>
                <%# Eval("Title") %>
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="Edit"
                  text="Edit"
                  commandname="Edit"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </itemtemplate>
        <edititemtemplate>
          <table>
            <tr>
              <td rowspan="4">
                <asp:image id="EmployeeEditImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b>Name:</b>
              </td>
              <td>
                <asp:textbox id="FirstNameUpdateTextBox"
                  text='<%# Bind("FirstName") %>'
                  runat="server"/>
                <asp:textbox id="LastNameUpdateTextBox"
                  text='<%# Bind("LastName") %>'
                  runat="server"/>
              </td>
            </tr>
            <tr>
              <td>
                <b>Title:</b>
              </td>
              <td>
                <asp:textbox id="TitleUpdateTextBox"
                  text='<%# Bind("Title") %>'
                  runat="server"/> 
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="UpdateButton"
                  text="Update"
                  commandname="Update"
                  runat="server"/>
                <asp:linkbutton id="CancelButton"
                  text="Cancel"
                  commandname="Cancel"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </edititemtemplate> 
                  
      </asp:formview>
      
      <br/><br/>
      
      <asp:Label id="MessageLabel"
        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="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title Where [EmployeeID]=@EmployeeID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

Commenti

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

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

Questo valore viene in genere impostato automaticamente dal FormView controllo quando viene fatto clic sul pulsante Nuovo, Aggiorna, Inserisci, Elimina o Annulla. Quando il FormView controllo modifica le modalità in risposta a un'azione, vengono generati gli eventi nella tabella seguente. Ciò consente di creare un gestore eventi personalizzato che esegue la routine appropriata quando si verifica l'evento.

Event Descrizione
ModeChanged Si verifica quando il FormView controllo cambia le modalità, ma dopo la modifica della modalità. Questo evento viene comunemente usato per eseguire un'attività ogni volta che il controllo modifica le FormView modalità.
ModeChanging Si verifica quando il FormView controllo cambia le modalità, ma prima della modifica della modalità. Questo evento viene comunemente usato per annullare la modifica della modalità.

Nota Questi eventi non vengono generati quando si modifica la modalità a livello di codice usando il ChangeMode metodo .

Si applica a

Vedi anche