注意:此类在 .NET Framework 2.0 版中是新增的。
使用用户定义的模板显示数据源中单个记录的值。使用
FormView 控件可以编辑、删除和插入记录。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)
<ControlValuePropertyAttribute("SelectedValue")> _
Public Class FormView
Inherits CompositeDataBoundControl
Implements IDataItemContainer, INamingContainer, IPostBackEventHandler, IPostBackContainer
[ControlValuePropertyAttribute("SelectedValue")]
public class FormView : CompositeDataBoundControl, IDataItemContainer, INamingContainer, IPostBackEventHandler,
IPostBackContainer
[ControlValuePropertyAttribute(L"SelectedValue")]
public ref class FormView : public CompositeDataBoundControl, IDataItemContainer, INamingContainer, IPostBackEventHandler,
IPostBackContainer
/** @attribute ControlValuePropertyAttribute("SelectedValue") */
public class FormView extends CompositeDataBoundControl implements IDataItemContainer, INamingContainer,
IPostBackEventHandler, IPostBackContainer
ControlValuePropertyAttribute("SelectedValue")
public class FormView extends CompositeDataBoundControl implements IDataItemContainer, INamingContainer,
IPostBackEventHandler, IPostBackContainer
FormView 控件用于显示数据源中的单个记录。该控件与 DetailsView 控件类似,只是它显示用户定义的模板而不是行字段。创建您自己的模板使您可以更灵活地控制数据的显示方式。FormView 控件支持以下功能:
模板
要使 FormView 控件显示内容,需要为该控件的不同部分创建模板。大多数模板是可选的;但是,必须为该控件的配置模式创建模板。例如,必须为支持插入记录的 FormView 控件定义插入项模板。下表列出了可以创建的不同模板。
若要在项模板中显示字段的值,请使用数据绑定表达式。有关数据绑定表达式的更多信息,请参见 数据绑定表达式语法。
使用双向绑定表达式可以将编辑项模板和插入项模板中的输入控件绑定到数据源的字段。这使得 FormView 控件可以自动提取输入控件的值以进行更新或插入操作。使用双向绑定表达式还可以使编辑项模板中的输入控件自动显示原始字段值。有关双向绑定表达式的更多信息,请参见 绑定到数据库。
绑定到数据
数据操作
FormView 控件提供许多内置功能,这些功能使用户可以对控件中的项进行更新、删除、插入和分页。FormView 控件绑定到数据源控件时,FormView 控件可以利用该数据源控件的功能并提供自动更新、删除、插入和分页功能。
注意 |
|---|
| FormView 控件可以为用其他类型的数据源进行更新、删除、插入和分页操作提供支持;但是,您必须提供一个适当的事件处理程序,其中包含对这些操作的实现。 |
因为 FormView 控件使用模板,所以该控件不提供自动生成命令按钮以执行更新、删除或插入操作的方法。必须手动将这些命令按钮包含在适当的模板中。FormView 控件识别某些 CommandName 属性设置为特定值的按钮。下表列出了 FormView 控件识别的命令按钮。
| 按钮 | Commandname 值 | 说明 |
| 取消 | “Cancel” | 在更新或插入操作中用于取消操作和放弃用户输入的值。然后 FormView 控件返回到 DefaultMode 属性指定的模式。 |
| 删除 | “Delete” | 在删除操作中用于从数据源中删除显示的记录。引发 ItemDeleting 和 ItemDeleted 事件。 |
| 编辑 | “Edit” | 在更新操作中用于使 FormView 控件处于编辑模式。在 EditItemTemplate 属性中指定的内容是为数据行显示的。 |
| 插入 | “Insert” | 在插入操作中用于尝试使用用户提供的值在数据源中插入新记录。引发 ItemInserting 和 ItemInserted 事件。 |
| 新建 | “New” | 在插入操作中用于使 FormView 控件处于插入模式。在 InsertItemTemplate 属性中指定的内容是为数据行显示的。 |
| 页 | “Page” | 在分页操作中用于表示页导航行中执行分页的按钮。若要指定分页操作,请将该按钮的 CommandArgument 属性设置为“Next”、“Prev”、“First”、“Last”或要导航至的目标页的索引。引发 PageIndexChanging 和 PageIndexChanged 事件。 注意 |
|---|
| 此类型的按钮通常只在页导航模板中使用。 |
|
| 更新 | “Update” | 在更新操作中用于尝试使用用户提供的值更新数据源中所显示的记录。引发 ItemUpdating 和 ItemUpdated 事件。 |
与“删除”按钮(该按钮立即删除所显示的记录)不同,单击“编辑”或“新建”按钮时,FormView 控件分别进入编辑模式或插入模式。在编辑模式中,EditItemTemplate 属性中包含的内容是为当前数据项而显示的。通常,编辑项模板被定义为用一个“更新”按钮和一个“取消”按钮替代“编辑”按钮。适合于字段的数据类型的输入控件(如 TextBox 或 CheckBox 控件)也通常与字段的值一起显示以便用户进行修改。单击“更新”按钮更新数据源中的记录,而单击“取消”按钮则放弃所有更改。
同样,当控件处于插入模式时,InsertItemTemplate 属性中包含的内容是为数据项而显示的。插入项模板通常定义为用一个“插入”按钮和一个“取消”按钮替代“新建”按钮,并显示空的输入控件以便用户输入新记录的值。单击“插入”按钮在数据源中插入记录,单击“取消”按钮放弃所有更改。
FormView 控件提供分页功能,该功能使用户可以导航至数据源中的其他记录。启用时,页导航行显示在包含页导航控件的 FormView 控件中。若要启用分页,请将 AllowPaging 属性设置为 true。您可以通过设置 PagerStyle 和 PagerSettings 属性中所包含的对象的属性自定义页导航行。可以不使用内置页导航行 UI,而使用 PagerTemplate 属性创建您自己的 UI。
自定义用户界面
您可以通过设置 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 控件和辅助功能。
下面的示例演示如何使用 FormView 控件显示 SqlDataSource 控件中的值。
<%@ 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") %> <%# 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>
<%@ 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") %> <%# 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 控件编辑现有记录。
<%@ 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">
</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">
</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>
<%@ 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">
</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">
</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 控件插入新记录。
<%@ 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">
</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">
</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>
<%@ 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">
</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">
</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>
System.Object
System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.BaseDataBoundControl
System.Web.UI.WebControls.DataBoundControl
System.Web.UI.WebControls.CompositeDataBoundControl
System.Web.UI.WebControls.FormView
此类型的任何公共静态(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