FormView.CurrentMode 属性

定义

获取 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

属性值

FormViewMode

FormViewMode 值之一。

属性

示例

下面的示例演示如何使用 CurrentMode 属性来确定控件是 FormView 处于编辑、插入还是只读模式。 如果用户尝试在控件编辑更多时 FormView 导航到另一条记录,则取消分页操作。


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

注解

使用 CurrentMode 属性确定控件是 FormView 处于编辑、插入还是只读模式。 下表列出了不同的模式值。

“模式” 说明
FormViewMode.Edit 控件 FormView 处于编辑模式,允许用户更新记录的值。
FormViewMode.Insert 控件 FormView 处于插入模式,允许用户向数据源添加新记录。
FormView.ReadOnly 控件 FormView 处于只读模式,这是正常的显示模式。

单击“新建”、“更新”、“插入”、“删除”或“取消”命令按钮时,通常由 FormView 控件自动设置此值。 当 FormView 控件更改模式以响应操作时,将引发下表中的事件。 这样,就可以创建自定义事件处理程序,该处理程序在事件发生时执行相应的例程。

事件 说明
ModeChanged FormView 控件更改模式时发生,但在模式更改后发生。 每当控件更改模式时 FormView ,此事件通常用于执行任务。
ModeChanging FormView 控件更改模式,但在模式更改之前发生。 此事件通常用于取消模式更改。

注意 使用 ChangeMode 该方法以编程方式更改模式时,不会引发这些事件。

适用于

另请参阅