FormView.DefaultMode 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置数据输入模式,FormView 控件在更新、插入或取消操作后返回到该模式。
public:
virtual property System::Web::UI::WebControls::FormViewMode DefaultMode { System::Web::UI::WebControls::FormViewMode get(); void set(System::Web::UI::WebControls::FormViewMode value); };
public virtual System.Web.UI.WebControls.FormViewMode DefaultMode { get; set; }
member this.DefaultMode : System.Web.UI.WebControls.FormViewMode with get, set
Public Overridable Property DefaultMode As FormViewMode
属性值
FormViewMode 值之一。 默认值为 FormViewMode.ReadOnly
。
例外
指定的值不是 FormViewMode 枚举值之一。
示例
以下示例演示如何在更新操作后使用 DefaultMode 属性在编辑模式下保留 FormView 控件。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FormView DefaultMode Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>FormView DefaultMode Example</h3>
<asp:formview id="EmployeeFormView"
datasourceid="EmployeeSource"
allowpaging="true"
datakeynames="EmployeeID"
defaultmode="Edit"
emptydatatext="No employees found."
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"/>
</td>
</tr>
</table>
</edititemtemplate>
</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]"
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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FormView DefaultMode Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>FormView DefaultMode Example</h3>
<asp:formview id="EmployeeFormView"
datasourceid="EmployeeSource"
allowpaging="true"
datakeynames="EmployeeID"
defaultmode="Edit"
emptydatatext="No employees found."
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"/>
</td>
</tr>
</table>
</edititemtemplate>
</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]"
updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title Where [EmployeeID]=@EmployeeID"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
注解
使用 DefaultMode 属性指定在执行更新、插入或取消操作后,控件是否 FormView 返回编辑、插入或只读模式。 默认情况下,控件 FormView 返回到只读模式;但是,通过将此属性设置为适当的值,可以将控件保持 FormView 编辑或插入模式。 下表列出了不同的模式值。
“模式” | 说明 |
---|---|
FormViewMode.Edit |
控件 FormView 处于编辑模式,允许用户更新记录的值。 |
FormViewMode.Insert |
控件 FormView 处于插入模式,允许用户向数据源添加新记录。 |
FormViewMode.ReadOnly |
控件 FormView 处于只读模式,这是正常的显示模式。 |
当控件具有单个用途(例如仅插入或编辑记录)时 FormView ,通常会使用此属性。