DetailsView.ChangeMode(DetailsViewMode) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将 DetailsView 控件切换为指定模式。
public:
void ChangeMode(System::Web::UI::WebControls::DetailsViewMode newMode);
public void ChangeMode (System.Web.UI.WebControls.DetailsViewMode newMode);
member this.ChangeMode : System.Web.UI.WebControls.DetailsViewMode -> unit
Public Sub ChangeMode (newMode As DetailsViewMode)
参数
- newMode
- DetailsViewMode
DetailsViewMode 值之一。
示例
下面的代码示例演示如何在用户导航到另一条记录时使用 ChangeMode 该方法将控件返回到 DetailsView 只读模式。
<%@ 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 CustomerDetailView_PageIndexChanged(Object sender, EventArgs e)
{
// By default, if the DetailsView control is in edit mode and
// user navigates to another page, the DetailsView control
// remains in edit mode. In this example, the ChangeMode
// method is used to put the DetailsView control in read-only
// mode whenever the user navigates to another record.
CustomerDetailView.ChangeMode(DetailsViewMode.ReadOnly);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView ChangeMode Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView ChangeMode Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogenerateeditbutton="true"
autogeneraterows="true"
allowpaging="true"
OnPageIndexChanged="CustomerDetailView_PageIndexChanged"
runat="server">
<fieldheaderstyle backcolor="Navy"
forecolor="White"/>
</asp:detailsview>
<!-- 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="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</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 CustomerDetailView_PageIndexChanged(ByVal sender As Object, _
ByVal e As EventArgs)
' By default, if the DetailsView control is in edit mode and
' user navigates to another page, the DetailsView control
' remains in edit mode. In this example, the ChangeMode
' method is used to put the DetailsView control in read-only
' mode whenever the user navigates to another record.
CustomerDetailView.ChangeMode(DetailsViewMode.ReadOnly)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView ChangeMode Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView ChangeMode Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogenerateeditbutton="true"
autogeneraterows="true"
allowpaging="true"
OnPageIndexChanged="CustomerDetailView_PageIndexChanged"
runat="server">
<fieldheaderstyle backcolor="Navy"
forecolor="White"/>
</asp:detailsview>
<!-- 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="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</form>
</body>
</html>
注解
ChangeMode使用此方法以编程方式在编辑、插入和只读模式之间切换DetailsView控件。 此方法还会使用指定的模式更新 CurrentMode 属性。 下表列出了不同的模式值。
“模式” | 说明 |
---|---|
DetailsViewMode.Edit |
控件 DetailsView 处于编辑模式,允许用户更新记录的值。 |
DetailsViewMode.Insert |
控件 DetailsView 处于插入模式,允许用户向数据源添加新记录。 |
DetailsView.ReadOnly |
控件 DetailsView 处于只读模式,这是正常的显示模式。 |