GridView.SelectedValue 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取 GridView 控件中选中行的数据键值。
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
属性值
GridView 控件中选中行的数据键值。
- 属性
示例
下面的示例演示如何使用 SelectedValue 属性来确定控件中 GridView 所选行的数据键值。
<%@ 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 CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Display the primary key value of the selected row.
Message.Text = "The primary key value of the selected row is " +
CustomersGridView.SelectedValue.ToString() + ".";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView SelectedValue Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView SelectedValue Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/><br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
allowpaging="true"
autogeneratecolumns="true"
autogenerateselectbutton="true"
datakeynames="CustomerID"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
runat="server">
<selectedrowstyle backcolor="LightBlue"
forecolor="DarkBlue"/>
</asp:gridview>
<!-- 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="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
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">
<script runat="server">
Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
' Display the primary key value of the selected row.
Message.Text = "The primary key value of the selected row is " & _
CustomersGridView.SelectedValue.ToString() & "."
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView SelectedValue Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView SelectedValue Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/><br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
allowpaging="true"
autogeneratecolumns="true"
autogenerateselectbutton="true"
datakeynames="CustomerID"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
runat="server">
<selectedrowstyle backcolor="LightBlue"
forecolor="DarkBlue"/>
</asp:gridview>
<!-- 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="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
注解
DataKeyNames使用表示数据源主键的逗号分隔字段名称列表设置属性时,该GridView控件使用指定字段或字段的值自动为控件中的每个行创建对象DataKey。 DataKey然后将对象添加到控件的DataKeys集合中。 通常,该 DataKeys 属性用于检索 DataKey 控件中特定数据行的对象 GridView 。 但是,如果只需要检索 DataKey 当前所选行的对象,只需将 SelectedDataKey 属性用作快捷方式。 作为进一步快捷方式,可以使用属性直接确定所选行 SelectedValue 的第一个键字段的数据键值。
如果要创建 ControlParameter 对象并想要访问第一个字段以外的键字段,请使用该 SelectedDataKey 属性。 有关示例,请参见 SelectedDataKey。