DataKey.Value 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 DataKey 物件中之索引 0 處的索引鍵欄位值。
public:
virtual property System::Object ^ Value { System::Object ^ get(); };
public virtual object Value { get; }
member this.Value : obj
Public Overridable ReadOnly Property Value As Object
屬性值
DataKey 中之索引 0 處的索引鍵欄位值。
範例
下列程式碼範例示範如何使用 Value 屬性來判斷記錄的主鍵值。
<%@ 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 CustomerDetailsView_DataBound(Object sender, EventArgs e)
{
// Get the DataKey object for the current record.
DataKey key = CustomerDetailsView.DataKey;
// Display the value of the key field.
MessageLabel.Text = "The key field value for the displayed record is " +
key.Value.ToString() + ".";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DataKey Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataKey Example</h3>
<asp:detailsview id="CustomerDetailsView"
datasourceid="DetailsViewSource"
autogeneraterows="true"
datakeynames="CustomerID"
allowpaging="true"
ondatabound="CustomerDetailsView_DataBound"
runat="server">
</asp:detailsview>
<br/>
<asp:label id="MessageLabel"
forecolor="Red"
runat="server"/>
<!-- 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"
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 CustomerDetailsView_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles CustomerDetailsView.DataBound
' Get the DataKey object for the current record.
Dim key As DataKey = CustomerDetailsView.DataKey
' Display the value of the key field.
MessageLabel.Text = "The key field value for the displayed record is " & _
key.Value.ToString() & "."
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DataKey Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataKey Example</h3>
<asp:detailsview id="CustomerDetailsView"
datasourceid="DetailsViewSource"
autogeneraterows="true"
datakeynames="CustomerID"
allowpaging="true"
runat="server">
</asp:detailsview>
<br/>
<asp:label id="MessageLabel"
forecolor="Red"
runat="server"/>
<!-- 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"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
備註
除了使用 Item[] 或 屬性從 DataKey 物件擷取索引鍵域值,您也可以使用 Value 屬性做為快捷方式,以判斷物件中 DataKey 索引 0 的索引 Values 鍵欄位值。 當記錄只有一個索引鍵欄位時,這個屬性通常用來判斷記錄的索引鍵域值。
注意
DataKey如果物件不包含任何索引鍵欄位, null
則會傳回 。