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 處於唯讀模式,這是一般顯示模式。 |