DetailsView.SelectedValue 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 DetailsView 控制項中目前資料錄的資料索引鍵值。
public:
property System::Object ^ SelectedValue { System::Object ^ get(); };
[System.ComponentModel.Browsable(false)]
public object SelectedValue { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedValue : obj
Public ReadOnly Property SelectedValue As Object
屬性值
DetailsView 控制項中目前資料錄的資料索引鍵值。
- 屬性
範例
下列程式代碼範例示範如何使用 SelectedValue 屬性來判斷目前記錄的數據索引鍵值。
<%@ 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_ItemUpdated(Object sender, DetailsViewUpdatedEventArgs e)
{
// Log the update operation using the key value contained
// in the SelectedValue property.
String keyValue = CustomerDetailView.SelectedValue.ToString();
LogUpdate(keyValue);
}
void LogUpdate(String keyValue)
{
// Insert code to log the update operation.
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView SelectedValue Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView SelectedValue Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneraterows="true"
autogenerateeditbutton="true"
allowpaging="true"
onitemupdated="CustomerDetailView_ItemUpdated"
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]"
UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName,
[Address] = @Address, [City] = @City,
[PostalCode] = @PostalCode, [Country] = @Country
WHERE [CustomerID] = @CustomerID">
</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_ItemUpdated(ByVal sender As Object, ByVal e As DetailsViewUpdatedEventArgs)
' Log the update operation using the key value contained
' in the SelectedValue property.
Dim keyValue As String = CustomerDetailView.SelectedValue.ToString()
LogUpdate(keyValue)
End Sub
Sub LogUpdate(ByVal keyValue As String)
' Insert code to log the update operation.
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
DetailsView SelectedValue Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>
DetailsView SelectedValue Example</h3>
<asp:DetailsView ID="CustomerDetailView" DataSourceID="DetailsViewSource" DataKeyNames="CustomerID"
AutoGenerateRows="true" AutoGenerateEditButton="true" AllowPaging="true" OnItemUpdated="CustomerDetailView_ItemUpdated"
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]"
UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName,
[Address] = @Address, [City] = @City,
[PostalCode] = @PostalCode, [Country] = @Country
WHERE [CustomerID] = @CustomerID"></asp:SqlDataSource>
</form>
</body>
</html>
備註
SelectedValue使用 屬性來判斷目前記錄的數據索引鍵值。 若要擷取代表記錄數據索引鍵的實際 System.Web.UI.WebControls.DataKey 物件,請使用 DataKey 屬性。
這個屬性是對象在運行時間系結至的預設屬性 ControlParameter 。 如需詳細資訊,請參閱 ControlValuePropertyAttribute、 ControlParameter和 搭配數據源控件使用參數以進行篩選。