GridView.SelectedDataKey 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
public:
virtual property System::Web::UI::WebControls::DataKey ^ SelectedDataKey { System::Web::UI::WebControls::DataKey ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.WebControls.DataKey SelectedDataKey { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedDataKey : System.Web.UI.WebControls.DataKey
Public Overridable ReadOnly Property SelectedDataKey As DataKey
속성 값
DataKey 컨트롤에서 선택한 행의 GridView입니다. 기본값은 null
로, 현재 선택된 행이 없음을 나타냅니다.
- 특성
예외
DataKeyNames 속성에 데이터 키가 지정되어 있지 않은 경우
예제
다음 예제에서는 컨트롤에서 SelectedDataKey 선택한 행 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.SelectedDataKey.Value.ToString() + ".";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView SelectedDataKey Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView SelectedDataKey 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.SelectedDataKey.Value.ToString() & "."
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView SelectedDataKey Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView SelectedDataKey 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>
다음 예제에서는 마스터/세부 정보 시나리오에서 두 번째 키 필드를 매개 변수로 사용하는 방법을 보여 줍니다. GridView 컨트롤은 Northwind 데이터베이스의 Order Details 테이블에서 레코드를 표시하는 데 사용됩니다. 컨트롤에서 레코드를 GridView 선택하면 Products 테이블의 제품 세부 정보가 컨트롤에 DetailsView 표시됩니다. ProductID는 컨트롤의 두 번째 키 이름입니다 GridView . 두 번째 키에 액세스하기 위해 GridView1.SelectedDataKey[1]의 DetailsView 값이 컨트롤 컨트롤의 SqlDataSource 개체로 ControlParameter 사용됩니다PropertyName.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Selecting Data Key Values</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [Order Details]">
</asp:SqlDataSource>
<asp:SqlDataSource
ID="SqlDataSource2"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [Products]WHERE [ProductID] = @productid">
<SelectParameters>
<asp:ControlParameter
Name="productid"
ControlID="GridView1"
PropertyName="SelectedDataKey[1]" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<asp:GridView
ID="GridView1"
runat="server"
AllowPaging="True"
AutoGenerateColumns="False"
DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True"/>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
<asp:BoundField DataField="Discount" HeaderText="Discount" />
</Columns>
</asp:GridView>
<br />
<asp:DetailsView
ID="DetailsView1"
runat="server"
AutoGenerateRows="False"
DataKeyNames="ProductID"
DataSourceID="SqlDataSource2"
Height="50px" Width="125px">
<Fields>
<asp:BoundField
DataField="ProductID"
HeaderText="ProductID"
InsertVisible="False"
ReadOnly="True" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"/>
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID"/>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" />
</Fields>
</asp:DetailsView>
</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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Selecting Data Key Values</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [Order Details]">
</asp:SqlDataSource>
<asp:SqlDataSource
ID="SqlDataSource2"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [Products]WHERE [ProductID] = @productid">
<SelectParameters>
<asp:ControlParameter
Name="productid"
ControlID="GridView1"
PropertyName="SelectedDataKey[1]" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<asp:GridView
ID="GridView1"
runat="server"
AllowPaging="True"
AutoGenerateColumns="False"
DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True"/>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
<asp:BoundField DataField="Discount" HeaderText="Discount" />
</Columns>
</asp:GridView>
<br />
<asp:DetailsView
ID="DetailsView1"
runat="server"
AutoGenerateRows="False"
DataKeyNames="ProductID"
DataSourceID="SqlDataSource2"
Height="50px" Width="125px">
<Fields>
<asp:BoundField
DataField="ProductID"
HeaderText="ProductID"
InsertVisible="False"
ReadOnly="True" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"/>
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID"/>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" />
</Fields>
</asp:DetailsView>
</form>
</body>
</html>
설명
속성이 DataKeyNames 설정 GridView 되면 컨트롤은 지정된 필드 또는 필드의 값이나 값을 사용하여 컨트롤의 각 행에 대한 개체를 자동으로 만듭니다 DataKey . DataKey 그런 다음 개체가 컨트롤의 DataKeys 컬렉션에 추가됩니다. 일반적으로 DataKeys 이 속성은 컨트롤의 DataKey 특정 데이터 행에 대한 개체를 검색하는 GridView 데 사용됩니다. 그러나 현재 선택한 행의 개체를 검색 DataKey 하기만 하면 되는 경우 이 속성을 바로 가기로 사용하면 SelectedDataKey 됩니다.
참고
컬렉션에서 DataKeys 속성에 지정된 인덱스에서 개체를 SelectedIndex 검색하는 DataKey 것과 같습니다. 이 속성을 사용하여 SelectedValue 현재 선택한 행의 데이터 키 값을 직접 검색할 수도 있습니다.
개체를 ControlParameter 만들고 첫 번째 필드가 아닌 키 필드에 액세스하려는 경우 개체의 ControlParameter 속성에 PropertyName 인덱싱된 SelectedDataKey 속성을 사용합니다. 아래에 예제가 나와 있습니다.