ListViewUpdateEventArgs.Keys 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得表示要更新之項目中索引鍵的欄位名稱/值組的字典。
public:
property System::Collections::Specialized::IOrderedDictionary ^ Keys { System::Collections::Specialized::IOrderedDictionary ^ get(); };
public System.Collections.Specialized.IOrderedDictionary Keys { get; }
member this.Keys : System.Collections.Specialized.IOrderedDictionary
Public ReadOnly Property Keys As IOrderedDictionary
屬性值
表示要更新之項目中索引鍵的欄位名稱/值組。
範例
下列範例示範如何使用 Keys 屬性來存取要更新之記錄的索引鍵字段值。
重要
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
<%@ 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 Page_Load()
{
Message.Text = String.Empty;
}
//<Snippet2>
void UnitMeasureListView_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
{
// Use the Keys property to retrieve the key field value
String keyValue = e.Keys[0].ToString().Trim();
// Cancel the update operation if the user attempts to
// edit a protected record. In this example, unit measure
// codes with 3 letters are protected.
if (keyValue.Length == 3)
{
Message.Text = "You cannot update this record. " +
" Unit Measure Code " + keyValue + " is protected.";
e.Cancel = true;
}
}
//</Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListViewUpdateEventArgs Keys Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListViewUpdateEventArgs Keys Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="UnitMeasureListView"
DataSourceID="UnitMeasureDataSource"
DataKeyNames="UnitMeasureCode"
OnItemUpdating="UnitMeasureListView_ItemUpdating"
runat="server">
<LayoutTemplate>
<table width="400px" border="1" id="tblUnit">
<tr runat="server">
<th runat="server"> </th>
<th runat="server">Unit Measure Code</th>
<th runat="server">Name</th>
</tr>
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
</td>
<td>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color:#B0C4DE" runat="server">
<td>
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
</td>
<td>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="background-color:#4682B4">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
</td>
<td>
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' MaxLength="50" /><br />
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="UnitMeasureDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [UnitMeasureCode], [Name] FROM Production.UnitMeasure"
UpdateCommand="UPDATE Production.UnitMeasure
SET [Name] = @Name
WHERE [UnitMeasureCode] = @UnitMeasureCode">
</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 Page_Load()
Message.Text = String.Empty
End Sub 'Page_Load
Sub UnitMeasureListView_ItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs)
' Use the Keys property to retrieve the key field value
Dim keyValue As String = e.Keys(0).ToString().Trim()
' Cancel the update operation if the user attempts to
' edit a protected record. In this example, unit measure
' codes with 3 letters are protected.
If keyValue.Length = 3 Then
Message.Text = "You cannot update this record. " & _
" Unit Measure Code " & keyValue & " is protected."
e.Cancel = True
End If
End Sub 'UnitMeasureListView_ItemUpdating
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListViewUpdateEventArgs Keys Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListViewUpdateEventArgs Keys Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="UnitMeasureListView"
DataSourceID="UnitMeasureDataSource"
DataKeyNames="UnitMeasureCode"
OnItemUpdating="UnitMeasureListView_ItemUpdating"
runat="server">
<LayoutTemplate>
<table width="400px" border="1" id="tblUnit">
<tr runat="server">
<th runat="server"> </th>
<th runat="server">Unit Measure Code</th>
<th runat="server">Name</th>
</tr>
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
</td>
<td>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color:#B0C4DE" runat="server">
<td>
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
</td>
<td>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="background-color:#4682B4">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
</td>
<td>
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' MaxLength="50" /><br />
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="UnitMeasureDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [UnitMeasureCode], [Name] FROM Production.UnitMeasure"
UpdateCommand="UPDATE Production.UnitMeasure
SET [Name] = @Name
WHERE [UnitMeasureCode] = @UnitMeasureCode">
</asp:SqlDataSource>
</form>
</body>
</html>
備註
DataKeyNames設定控制元件的 ListView 屬性時,請使用 Keys 屬性 (字典) 來存取專案中要更新的主鍵值。
字典 Keys 會自動填入 屬性中所 DataKeyNames 指定欄位或欄位的名稱/值組。 如果多個字段形成主鍵,則會將個別專案新增至 Keys 每個索引鍵字段的字典。
若要判斷索引鍵欄位的名稱,請使用DictionaryEntry.Key字典中包含的 Keys 物件屬性DictionaryEntry。 若要判斷索引鍵欄位的值,請使用 DictionaryEntry.Value 屬性。