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和 将参数用于筛选的数据源控件。