请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
FormView 类

  开启低带宽视图
此页面仅适用于
Microsoft Visual Studio 2005/.Net Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
FormView 类

注意:此类在 .NET Framework 2.0 版中是新增的。

使用用户定义的模板显示数据源中单个记录的值。使用 FormView 控件可以编辑、删除和插入记录。

命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)

Visual Basic(声明)
<ControlValuePropertyAttribute("SelectedValue")> _
Public Class FormView
    Inherits CompositeDataBoundControl
    Implements IDataItemContainer, INamingContainer, IPostBackEventHandler, IPostBackContainer
Visual Basic(用法)
Dim instance As FormView
C#
[ControlValuePropertyAttribute("SelectedValue")] 
public class FormView : CompositeDataBoundControl, IDataItemContainer, INamingContainer, IPostBackEventHandler, 
    IPostBackContainer
C++
[ControlValuePropertyAttribute(L"SelectedValue")] 
public ref class FormView : public CompositeDataBoundControl, IDataItemContainer, INamingContainer, IPostBackEventHandler, 
    IPostBackContainer
J#
/** @attribute ControlValuePropertyAttribute("SelectedValue") */ 
public class FormView extends CompositeDataBoundControl implements IDataItemContainer, INamingContainer, 
    IPostBackEventHandler, IPostBackContainer
JScript
ControlValuePropertyAttribute("SelectedValue") 
public class FormView extends CompositeDataBoundControl implements IDataItemContainer, INamingContainer, 
    IPostBackEventHandler, IPostBackContainer

FormView 控件用于显示数据源中的单个记录。该控件与 DetailsView 控件类似,只是它显示用户定义的模板而不是行字段。创建您自己的模板使您可以更灵活地控制数据的显示方式。FormView 控件支持以下功能:

  • 绑定到数据源控件,如 SqlDataSourceObjectDataSource

  • 内置插入功能。

  • 内置更新和删除功能。

  • 内置分页功能。

  • 以编程方式访问 FormView 对象模型以动态设置属性、处理事件等。

  • 可通过用户定义的模板、主题和样式自定义外观。

模板

要使 FormView 控件显示内容,需要为该控件的不同部分创建模板。大多数模板是可选的;但是,必须为该控件的配置模式创建模板。例如,必须为支持插入记录的 FormView 控件定义插入项模板。下表列出了可以创建的不同模板。

模板类型

说明

EditItemTemplate

定义数据行在 FormView 控件处于编辑模式时的内容。此模板通常包含用户可以用来编辑现有记录的输入控件和命令按钮。

EmptyDataTemplate

定义在 FormView 控件绑定到不包含任何记录的数据源时所显示的空数据行的内容。此模板通常包含用来警告用户数据源不包含任何记录的内容。

FooterTemplate

定义脚注行的内容。此模板通常包含任何要在脚注行中显示的附加内容。

Note注意

另一种方法是可以通过设置 FooterText 属性来指定要在脚注行中显示的文本。

HeaderTemplate

定义标题行的内容。此模板通常包含任何要在标题行中显示的附加内容。

Note注意

另一种方法是可以通过设置 HeaderText 属性来指定要在标题行中显示的文本。

ItemTemplate

定义数据行在 FormView 控件处于只读模式时的内容。此模板通常包含用来显示现有记录的值的内容。

InsertItemTemplate

定义数据行在 FormView 控件处于插入模式时的内容。此模板通常包含用户可以用来添加新记录的输入控件和命令按钮。

PagerTemplate

定义在启用分页功能时(即 AllowPaging 属性设置为 true 时)所显示的页导航行的内容。此模板通常包含用户可以用来导航至另一个记录的控件。

Note注意

FormView 控件具有内置页导航行用户界面 (UI)。仅当希望创建您自己的自定义页导航行时才需要创建页导航模板。

若要在项模板中显示字段的值,请使用数据绑定表达式。有关数据绑定表达式的更多信息,请参见 数据绑定表达式语法

使用双向绑定表达式可以将编辑项模板和插入项模板中的输入控件绑定到数据源的字段。这使得 FormView 控件可以自动提取输入控件的值以进行更新或插入操作。使用双向绑定表达式还可以使编辑项模板中的输入控件自动显示原始字段值。有关双向绑定表达式的更多信息,请参见 绑定到数据库

绑定到数据

FormView 控件可以绑定到数据源控件(如 SqlDataSourceAccessDataSourceObjectDataSource 等),或绑定到实现 System.Collections.IEnumerable 接口的任何数据源(如 System.Data.DataViewSystem.Collections.ArrayListSystem.Collections.Hashtable)。使用以下方法之一将 FormView 控件绑定到适当的数据源类型:

  • 若要绑定到数据源控件,请将 FormView 控件的 DataSourceID 属性设置为数据源控件的 ID 值。FormView 控件自动绑定到指定的数据源控件,并且可以利用数据源控件的功能执行插入、更新、删除和分页功能。这是绑定到数据的首选方法。

  • 若要绑定到实现 System.Collections.IEnumerable 接口的数据源,请以编程方式将 FormView 控件的 DataSource 属性设置为该数据源,然后调用 DataBind 方法。使用此方法时,FormView 控件不提供内置插入、更新、删除和分页功能。需要使用适当的事件提供此功能。

有关数据绑定的更多信息,请参见 通过 ASP.NET 访问数据

Note注意

此控件可用来显示用户输入,而该输入可能包含恶意的客户端脚本。在应用程序中显示从客户端发送来的任何信息之前,请检查它们是否包含可执行脚本、SQL 语句或其他代码。只要有可能,强烈建议在此控件中显示值之前,对它们进行 HTML 编码。ASP.NET 提供输入请求验证功能以阻止用户输入中的脚本和 HTML。还提供验证服务器控件以判断用户输入。有关更多信息,请参见 验证控件介绍

数据操作

FormView 控件提供许多内置功能,这些功能使用户可以对控件中的项进行更新、删除、插入和分页。FormView 控件绑定到数据源控件时,FormView 控件可以利用该数据源控件的功能并提供自动更新、删除、插入和分页功能。

Note注意

FormView 控件可以为用其他类型的数据源进行更新、删除、插入和分页操作提供支持;但是,您必须提供一个适当的事件处理程序,其中包含对这些操作的实现。

因为 FormView 控件使用模板,所以该控件不提供自动生成命令按钮以执行更新、删除或插入操作的方法。必须手动将这些命令按钮包含在适当的模板中。FormView 控件识别某些 CommandName 属性设置为特定值的按钮。下表列出了 FormView 控件识别的命令按钮。

按钮

Commandname 值

说明

取消

“Cancel”

在更新或插入操作中用于取消操作和放弃用户输入的值。然后 FormView 控件返回到 DefaultMode 属性指定的模式。

删除

“Delete”

在删除操作中用于从数据源中删除显示的记录。引发 ItemDeletingItemDeleted 事件。

编辑

“Edit”

在更新操作中用于使 FormView 控件处于编辑模式。在 EditItemTemplate 属性中指定的内容是为数据行显示的。

插入

“Insert”

在插入操作中用于尝试使用用户提供的值在数据源中插入新记录。引发 ItemInsertingItemInserted 事件。

新建

“New”

在插入操作中用于使 FormView 控件处于插入模式。在 InsertItemTemplate 属性中指定的内容是为数据行显示的。

“Page”

在分页操作中用于表示页导航行中执行分页的按钮。若要指定分页操作,请将该按钮的 CommandArgument 属性设置为“Next”、“Prev”、“First”、“Last”或要导航至的目标页的索引。引发 PageIndexChangingPageIndexChanged 事件。

Note注意

此类型的按钮通常只在页导航模板中使用。

更新

“Update”

在更新操作中用于尝试使用用户提供的值更新数据源中所显示的记录。引发 ItemUpdatingItemUpdated 事件。

与“删除”按钮(该按钮立即删除所显示的记录)不同,单击“编辑”或“新建”按钮时,FormView 控件分别进入编辑模式或插入模式。在编辑模式中,EditItemTemplate 属性中包含的内容是为当前数据项而显示的。通常,编辑项模板被定义为用一个“更新”按钮和一个“取消”按钮替代“编辑”按钮。适合于字段的数据类型的输入控件(如 TextBoxCheckBox 控件)也通常与字段的值一起显示以便用户进行修改。单击“更新”按钮更新数据源中的记录,而单击“取消”按钮则放弃所有更改。

同样,当控件处于插入模式时,InsertItemTemplate 属性中包含的内容是为数据项而显示的。插入项模板通常定义为用一个“插入”按钮和一个“取消”按钮替代“新建”按钮,并显示空的输入控件以便用户输入新记录的值。单击“插入”按钮在数据源中插入记录,单击“取消”按钮放弃所有更改。

FormView 控件提供分页功能,该功能使用户可以导航至数据源中的其他记录。启用时,页导航行显示在包含页导航控件的 FormView 控件中。若要启用分页,请将 AllowPaging 属性设置为 true。您可以通过设置 PagerStylePagerSettings 属性中所包含的对象的属性自定义页导航行。可以不使用内置页导航行 UI,而使用 PagerTemplate 属性创建您自己的 UI。

自定义用户界面

您可以通过设置 FormView 控件的不同部分的样式属性自定义该控件的外观。下表列出了不同的样式属性。

样式属性

说明

EditRowStyle

FormView 控件处于编辑模式时数据行的样式设置。

EmptyDataRowStyle

数据源不包含任何记录时 FormView 控件中显示的空数据行的样式设置。

FooterStyle

FormView 控件的脚注行的样式设置。

HeaderStyle

FormView 控件的标题行的样式设置。

InsertRowStyle

FormView 控件处于插入模式时数据行的样式设置。

PagerStyle

启用分页功能时 FormView 控件中显示的页导航行的样式设置。

RowStyle

FormView 控件处于只读模式时数据行的样式设置。

事件

FormView 控件提供多个您可以对其进行编程的事件。这使您可以在每次发生事件时都运行一个自定义例程。下表列出了 FormView 控件支持的事件。

事件

说明

ItemCommand

在单击 FormView 控件中的按钮时发生。此事件通常用于在控件中单击按钮时执行某项任务。

ItemCreated

FormView 控件中创建了所有 FormViewRow 对象之后发生。此事件通常用于在显示记录前修改该记录的值。

ItemDeleted

在单击“删除”按钮(CommandName 属性设置为“Delete”的按钮)时,但在 FormView 控件从数据源中删除该记录之后发生。此事件通常用于检查删除操作的结果。

ItemDeleting

在单击“删除”按钮时,但在 FormView 控件从数据源中删除该记录之前发生。此事件通常用于取消删除操作。

ItemInserted

在单击“插入”按钮(CommandName 属性设置为“Insert”的按钮)时,但在 FormView 控件插入记录之后发生。此事件通常用于检查插入操作的结果。

ItemInserting

在单击“插入”按钮时,但在 FormView 控件插入记录之前发生。此事件通常用于取消插入操作。

ItemUpdated

在单击“更新”按钮(CommandName 属性设置为“Update”的按钮)时,但在 FormView 控件更新行之后发生。此事件通常用于检查更新操作的结果。

ItemUpdating

在单击“更新”按钮时,但在 FormView 控件更新记录之前发生。此事件通常用于取消更新操作。

ModeChanged

FormView 控件更改模式(更改为编辑、插入或只读模式)之后发生。此事件通常用于在 FormView 控件更改模式时执行某项任务。

ModeChanging

FormView 控件更改模式(更改为编辑、插入或只读模式)之前发生。此事件通常用于取消模式更改。

PageIndexChanged

在单击某一页导航按钮时,但在 FormView 控件处理分页操作之后发生。当您在用户定位到控件中不同的记录后需要执行任务时,通常使用此事件。

PageIndexChanging

在单击某一页导航按钮时,但在 FormView 控件处理分页操作之前发生。此事件通常用于取消分页操作。

辅助功能

默认情况下,为此控件呈现的标记可能不符合辅助功能标准,例如 Web 内容辅助功能准则 1.0 (WCAG) 优先级 1 准则。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能

TopicLocation
如何:以声明方式创建 ASP.NET Web 控件模板生成 ASP .NET Web 应用程序
如何:以声明方式创建 ASP.NET Web 控件模板在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:创建 ASP.NET Web 服务器控件模板生成 ASP .NET Web 应用程序
如何:动态创建 ASP.NET Web 服务器控件模板生成 ASP .NET Web 应用程序
如何:动态创建 ASP.NET Web 服务器控件模板在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应数据绑定控件中的按钮事件生成 ASP .NET Web 应用程序
如何:响应数据绑定控件中的按钮事件在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在模板化控件中绑定到数据生成 ASP .NET Web 应用程序
如何:在模板化控件中绑定到数据在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:绑定到 Visual Studio 的模板化控件中的数据在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:绑定到 Visual Studio 的模板化控件中的数据在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:使用 FormView Web 服务器控件在网页中显示格式化数据在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:使用 FormView Web 服务器控件在网页中显示格式化数据在 Visual Studio 中生成 ASP .NET Web 应用程序

下面的示例演示如何使用 FormView 控件显示 SqlDataSource 控件中的值。

Visual Basic
<%@ Page language="VB" %>

<html>
  <body>
    <form runat="server">
        
      <h3>FormView Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID" 
        runat="server">
        
        <itemtemplate>
        
          <table>
            <tr>
              <td>
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td>
                <h3><%# Eval("FirstName") %>&nbsp;<%# Eval("LastName") %></h3>      
                <%# Eval("Title") %>        
              </td>
            </tr>
          </table>
        
        </itemtemplate>
          
        <pagersettings position="Bottom"
          mode="NextPrevious"/> 
                  
      </asp:formview>
          
      <!-- 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]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

C#
<%@ Page language="C#" %>

<html>
  <body>
    <form runat="server">
        
      <h3>FormView Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID" 
        runat="server">
        
        <itemtemplate>
        
          <table>
            <tr>
              <td>
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td>
                <h3><%# Eval("FirstName") %>&nbsp;<%# Eval("LastName") %></h3>      
                <%# Eval("Title") %>        
              </td>
            </tr>
          </table>
        
        </itemtemplate>
          
        <pagersettings position="Bottom"
          mode="NextPrevious"/> 
                  
      </asp:formview>
          
      <!-- 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]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

下面的示例演示如何使用 FormView 控件编辑现有记录。

Visual Basic
<%@ Page language="VB" %>

<script runat="server">

  Sub EmployeeFormView_ItemUpdating(ByVal sender As Object, ByVal e As FormViewUpdateEventArgs) Handles EmployeeFormView.ItemUpdating
  
    ' Validate the field values entered by the user. This
    ' example determines whether the user left any fields
    ' empty. Use the NewValues property to access the new 
    ' values entered by the user.
        Dim emptyFieldList As ArrayList = ValidateFields(e.NewValues)

    If emptyFieldList.Count > 0 Then

      ' The user left some fields empty. Display an error message.
      
      ' Use the Keys property to retrieve the key field value.
      Dim keyValue As String = e.Keys("EmployeeID").ToString()

      MessageLabel.Text = "You must enter a value for each field of record " & _
        keyValue & ".<br/>The following fields are missing:<br/><br/>"

      ' Display the missing fields.
      Dim value As String
      For Each value In emptyFieldList
      
        ' Use the OldValues property to access the original value
        ' of a field.
        MessageLabel.Text &= value & " - Original Value = " & _
          e.OldValues(value).ToString() & "<br>"
        
      Next

      ' Cancel the update operation.
      e.Cancel = True

    Else
    
      ' The field values passed validation. Clear the
      ' error message label.
      MessageLabel.Text = ""
      
    End If

  End Sub

  Function ValidateFields(ByVal list As IOrderedDictionary) As ArrayList
    
    ' Create an ArrayList object to store the
    ' names of any empty fields.
    Dim emptyFieldList As New ArrayList()

    ' Iterate though the field values entered by
    ' the user and check for an empty field. Empty
    ' fields contain a null value.
    Dim entry As DictionaryEntry
    
    For Each entry In list
    
      If entry.Value Is String.Empty Then
      
        ' Add the field name to the ArrayList object.
        emptyFieldList.Add(entry.Key.ToString())
        
      End If
      
    Next

    Return emptyFieldList
  
  End Function
  
  Sub EmployeeFormView_ModeChanging(ByVal sender As Object, ByVal e As FormViewModeEventArgs) Handles EmployeeFormView.ModeChanging
  
    If e.CancelingEdit Then
      
      ' The user canceled the update operation.
      ' Clear the error message label.
      MessageLabel.Text = ""
    
    End If
    
  End Sub
  
</script>

<html>
  <body>
    <form runat="server">
        
      <h3>FormView Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        headertext="Employee Record"
        emptydatatext="No employees found."
        runat="server">
        
        <headerstyle backcolor="CornFlowerBlue"
          forecolor="White"
          font-size="14"
          horizontalalign="Center"  
          wrap="false"/>
        <rowstyle backcolor="LightBlue"
          wrap="false"/>
        <pagerstyle backcolor="CornFlowerBlue"/>

        <itemtemplate>
          <table>
            <tr>
              <td rowspan="6">
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td colspan="2">
                  &nbsp; 
              </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>
                <b>Hire Date:</b>                 
              </td>
              <td>
                <%# Eval("HireDate","{0:d}") %>
              </td>
            </tr>
            <tr height="150" valign="top">
              <td>
                <b>Address:</b>
              </td>
              <td>
                <%# Eval("Address") %><br/>
                <%# Eval("City") %> <%# Eval("Region") %>
                <%# Eval("PostalCode") %><br/>
                <%# Eval("Country") %>   
              </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="6">
                <asp:image id="EmployeeEditImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td colspan="2">
                  &nbsp; 
              </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>
                <b>Hire Date:</b>                 
              </td>
              <td>
                <asp:textbox id="HireDateUpdateTextBox"
                  text='<%# Bind("HireDate", "{0:d}") %>'
                  runat="server"/>
              </td>
            </tr>
            <tr height="150" valign="top">
              <td>
                <b>Address:</b>
              </td>
              <td>
                <asp:textbox id="AddressUpdateTextBox"
                  text='<%# Bind("Address") %>'
                  runat="server"/>
                <br/>
                <asp:textbox id="CityUpdateTextBox"
                  text='<%# Bind("City") %>'
                  runat="server"/> 
                <asp:textbox id="RegionUpdateTextBox"
                  text='<%# Bind("Region") %>'
                  width="40"
                  runat="server"/>
                <asp:textbox id="PostalCodeUpdateTextBox"
                  text='<%# Bind("PostalCode") %>'
                  width="60"
                  runat="server"/>
                <br/>
                <asp:textbox id="CountryUpdateTextBox"
                  text='<%# Bind("Country") %>'
                  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>
          
        <pagersettings position="Bottom"
          mode="Numeric"/> 
                  
      </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], [Address], [City], [Region], [PostalCode], [Country], [HireDate], [PhotoPath] From [Employees]"
        updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title, [Address]=@Address, [City]=@City, [Region]=@Region, [PostalCode]=@PostalCode, [Country]=@Country Where [EmployeeID]=@EmployeeID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

C#
<%@ Page language="C#" %>

<script runat="server">

  void EmployeeFormView_ItemUpdating(Object sender, FormViewUpdateEventArgs e)
  {

    // Validate the field values entered by the user. This
    // example determines whether the user left any fields
    // empty. Use the NewValues property to access the new 
    // values entered by the user.
    ArrayList emptyFieldList = ValidateFields(e.NewValues);

    if (emptyFieldList.Count > 0)
    {

      // The user left some fields empty. Display an error message.
      
      // Use the Keys property to retrieve the key field value.
      String keyValue = e.Keys["EmployeeID"].ToString();

      MessageLabel.Text = "You must enter a value for each field of record " +
        keyValue + ".<br/>The following fields are missing:<br/><br/>";

      // Display the missing fields.
      foreach (String value in emptyFieldList)
      {
        // Use the OldValues property to access the original value
        // of a field.
        MessageLabel.Text += value + " - Original Value = " + 
          e.OldValues[value].ToString() + "<br>";
      }

      // Cancel the update operation.
      e.Cancel = true;

    }
    else
    {
      // The field values passed validation. Clear the
      // error message label.
      MessageLabel.Text = "";
    }

  }

  ArrayList ValidateFields(IOrderedDictionary list)
  {
    
    // Create an ArrayList object to store the
    // names of any empty fields.
    ArrayList emptyFieldList = new ArrayList();

    // Iterate though the field values entered by
    // the user and check for an empty field. Empty
    // fields contain a null value.
    foreach (DictionaryEntry entry in list)
    {
      if (entry.Value == String.Empty)
      {
        // Add the field name to the ArrayList object.
        emptyFieldList.Add(entry.Key.ToString());
      }
    }

    return emptyFieldList;
  }

  void EmployeeFormView_ModeChanging(Object sender, FormViewModeEventArgs e)
  {
    if (e.CancelingEdit)
    {
      // The user canceled the update operation.
      // Clear the error message label.
      MessageLabel.Text = "";
    }
  }

</script>

<html>
  <body>
    <form runat="server">
        
      <h3>FormView Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        headertext="Employee Record"
        emptydatatext="No employees found."
        onitemupdating="EmployeeFormView_ItemUpdating"
        onmodechanging="EmployeeFormView_ModeChanging"  
        runat="server">
        
        <headerstyle backcolor="CornFlowerBlue"
          forecolor="White"
          font-size="14"
          horizontalalign="Center"  
          wrap="false"/>
        <rowstyle backcolor="LightBlue"
          wrap="false"/>
        <pagerstyle backcolor="CornFlowerBlue"/>

        <itemtemplate>
          <table>
            <tr>
              <td rowspan="6">
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td colspan="2">
                  &nbsp; 
              </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>
                <b>Hire Date:</b>                 
              </td>
              <td>
                <%# Eval("HireDate","{0:d}") %>
              </td>
            </tr>
            <tr height="150" valign="top">
              <td>
                <b>Address:</b>
              </td>
              <td>
                <%# Eval("Address") %><br/>
                <%# Eval("City") %> <%# Eval("Region") %>
                <%# Eval("PostalCode") %><br/>
                <%# Eval("Country") %>   
              </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="6">
                <asp:image id="EmployeeEditImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td colspan="2">
                  &nbsp; 
              </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>
                <b>Hire Date:</b>                 
              </td>
              <td>
                <asp:textbox id="HireDateUpdateTextBox"
                  text='<%# Bind("HireDate", "{0:d}") %>'
                  runat="server"/>
              </td>
            </tr>
            <tr height="150" valign="top">
              <td>
                <b>Address:</b>
              </td>
              <td>
                <asp:textbox id="AddressUpdateTextBox"
                  text='<%# Bind("Address") %>'
                  runat="server"/>
                <br/>
                <asp:textbox id="CityUpdateTextBox"
                  text='<%# Bind("City") %>'
                  runat="server"/> 
                <asp:textbox id="RegionUpdateTextBox"
                  text='<%# Bind("Region") %>'
                  width="40"
                  runat="server"/>
                <asp:textbox id="PostalCodeUpdateTextBox"
                  text='<%# Bind("PostalCode") %>'
                  width="60"
                  runat="server"/>
                <br/>
                <asp:textbox id="CountryUpdateTextBox"
                  text='<%# Bind("Country") %>'
                  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>
          
        <pagersettings position="Bottom"
          mode="Numeric"/> 
                  
      </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], [Address], [City], [Region], [PostalCode], [Country], [HireDate], [PhotoPath] From [Employees]"
        updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title, [Address]=@Address, [City]=@City, [Region]=@Region, [PostalCode]=@PostalCode, [Country]=@Country Where [EmployeeID]=@EmployeeID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

下面的示例演示如何使用 FormView 控件插入新记录。

Visual Basic
<%@ Page language="VB" %>

<html>
  <body>
    <form runat="server">
        
      <h3>FormView InsertItemTemplate Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        emptydatatext="No employees found."  
        runat="server">
        
        <rowstyle backcolor="LightGreen"
          wrap="false"/>
        <insertrowstyle backcolor="LightBlue"
          wrap="false"/>

        <itemtemplate>
          <table>
            <tr>
              <td rowspan="5">
                <asp:image id="CompanyLogoImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company Logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                  &nbsp; 
              </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="NewButton"
                  text="New"
                  commandname="New"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </itemtemplate>
        <insertitemtemplate>
          <table>
            <tr>
              <td rowspan="4">
                <asp:image id="CompanyLogoEditImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company Logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                  &nbsp; 
              </td>
            </tr>
            <tr>
              <td>
                <b>Name:</b>
              </td>
              <td>
                <asp:textbox id="FirstNameInsertTextBox"
                  text='<%# Bind("FirstName") %>'
                  runat="server"/>
                <asp:textbox id="LastNameInsertTextBox"
                  text='<%# Bind("LastName") %>'
                  runat="server"/>
              </td>
            </tr>
            <tr>
              <td>
                <b>Title:</b>
              </td>
              <td>
                <asp:textbox id="TitleInsertTextBox"
                  text='<%# Bind("Title") %>'
                  runat="server"/> 
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="InsertButton"
                  text="Insert"
                  commandname="Insert"
                  runat="server"/>
                <asp:linkbutton id="CancelButton"
                  text="Cancel"
                  commandname="Cancel"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </insertitemtemplate> 
                  
      </asp:formview>

      <!-- 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]"
        insertcommand="Insert Into [Employees] ([LastName], [FirstName], [Title]) VALUES (@LastName, @FirstName, @Title)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

C#
<%@ Page language="C#" %>

<html>
  <body>
    <form runat="server">
        
      <h3>FormView InsertItemTemplate Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        emptydatatext="No employees found."  
        runat="server">
        
        <rowstyle backcolor="LightGreen"
          wrap="false"/>
        <insertrowstyle backcolor="LightBlue"
          wrap="false"/>

        <itemtemplate>
          <table>
            <tr>
              <td rowspan="5">
                <asp:image id="CompanyLogoImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company Logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                  &nbsp; 
              </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="NewButton"
                  text="New"
                  commandname="New"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </itemtemplate>
        <insertitemtemplate>
          <table>
            <tr>
              <td rowspan="4">
                <asp:image id="CompanyLogoEditImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company Logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                  &nbsp; 
              </td>
            </tr>
            <tr>
              <td>
                <b>Name:</b>
              </td>
              <td>
                <asp:textbox id="FirstNameInsertTextBox"
                  text='<%# Bind("FirstName") %>'
                  runat="server"/>
                <asp:textbox id="LastNameInsertTextBox"
                  text='<%# Bind("LastName") %>'
                  runat="server"/>
              </td>
            </tr>
            <tr>
              <td>
                <b>Title:</b>
              </td>
              <td>
                <asp:textbox id="TitleInsertTextBox"
                  text='<%# Bind("Title") %>'
                  runat="server"/> 
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="InsertButton"
                  text="Insert"
                  commandname="Insert"
                  runat="server"/>
                <asp:linkbutton id="CancelButton"
                  text="Cancel"
                  commandname="Cancel"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </insertitemtemplate> 
                  
      </asp:formview>

      <!-- 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]"
        insertcommand="Insert Into [Employees] ([LastName], [FirstName], [Title]) VALUES (@LastName, @FirstName, @Title)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker