FormView.CurrentMode Właściwość

Definicja

Pobiera bieżący tryb wprowadzania danych kontrolki 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

Wartość właściwości

FormViewMode

FormViewMode Jedna z wartości.

Atrybuty

Przykłady

W poniższym przykładzie pokazano, jak za pomocą CurrentMode właściwości określić, czy kontrolka FormView jest w trybie edycji, wstawiania lub tylko do odczytu. Jeśli użytkownik spróbuje przejść do innego rekordu, gdy kontrolka FormView będzie edytować więcej, operacja stronicowania zostanie anulowana.


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

Uwagi

CurrentMode Użyj właściwości , aby określić, czy kontrolka FormView jest w trybie edycji, wstawiania lub tylko do odczytu. W poniższej tabeli wymieniono różne wartości trybu.

Tryb Opis
FormViewMode.Edit Kontrolka FormView jest w trybie edycji, który umożliwia użytkownikowi aktualizowanie wartości rekordu.
FormViewMode.Insert Kontrolka FormView jest w trybie wstawiania, który umożliwia użytkownikowi dodanie nowego rekordu do źródła danych.
FormView.ReadOnly Kontrolka FormView jest w trybie tylko do odczytu, który jest normalnym trybem wyświetlania.

Ta wartość jest zwykle ustawiana automatycznie przez kontrolkę po kliknięciu FormView przycisku polecenia Nowy, Aktualizuj, Wstaw, Usuń lub Anuluj. Gdy kontrolka FormView zmienia tryby w odpowiedzi na akcję, są zgłaszane zdarzenia w poniższej tabeli. Dzięki temu można utworzyć niestandardową procedurę obsługi zdarzeń, która wykonuje odpowiednią procedurę po wystąpieniu zdarzenia.

Zdarzenie Opis
ModeChanged Występuje, gdy kontrolka FormView zmienia tryby, ale po zmianie trybu. To zdarzenie jest często używane do wykonywania zadania za każdym razem, gdy kontrolka FormView zmienia tryby.
ModeChanging Występuje, gdy kontrolka FormView zmienia tryby, ale przed zmianą trybu. To zdarzenie jest często używane do anulowania zmiany trybu.

Uwaga Te zdarzenia nie są wywoływane podczas programowego zmieniania trybu przy użyciu ChangeMode metody .

Dotyczy

Zobacz też