GridView.SelectedValue Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan nilai kunci data dari baris yang dipilih dalam GridView kontrol.
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
Nilai Properti
Nilai kunci data dari baris yang dipilih dalam GridView kontrol.
- Atribut
Contoh
Contoh berikut menunjukkan cara menggunakan SelectedValue properti untuk menentukan nilai kunci data baris yang dipilih dalam GridView kontrol.
<%@ 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>
Keterangan
DataKeyNames Saat properti diatur dengan daftar nama bidang yang dipisahkan koma yang mewakili kunci utama sumber data, GridView kontrol secara otomatis membuat DataKey objek untuk setiap baris dalam kontrol menggunakan nilai atau nilai bidang atau bidang yang ditentukan. Objek DataKey kemudian ditambahkan ke koleksi kontrol DataKeys . Biasanya, DataKeys properti digunakan untuk mengambil DataKey objek untuk baris data tertentu dalam GridView kontrol. Namun, jika Anda hanya perlu mengambil DataKey objek dari baris yang saat ini dipilih, Anda cukup menggunakan SelectedDataKey properti sebagai pintasan. Sebagai pintasan lebih lanjut, Anda dapat langsung menentukan nilai kunci data dari bidang kunci pertama dari baris yang dipilih dengan menggunakan SelectedValue properti .
Jika Anda membuat ControlParameter objek dan ingin mengakses bidang kunci selain bidang pertama, gunakan SelectedDataKey properti . Misalnya, lihat SelectedDataKey.