ListViewUpdateEventArgs 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供 ItemUpdating 事件的資料。
public ref class ListViewUpdateEventArgs : System::ComponentModel::CancelEventArgs
public class ListViewUpdateEventArgs : System.ComponentModel.CancelEventArgs
type ListViewUpdateEventArgs = class
inherit CancelEventArgs
Public Class ListViewUpdateEventArgs
Inherits CancelEventArgs
- 繼承
範例
下列範例示範如何使用 NewValues 屬性,確保使用者已在更新數據源之前提供所有值。
重要
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,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 ContactsListView_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
{
// Cancel the update operation if any of the fields is empty
// or null.
foreach (DictionaryEntry de in e.NewValues)
{
// Check if the value is null or empty.
if (de.Value == null || de.Value.ToString().Trim().Length == 0)
{
Message.Text = "Cannot set a field to an empty value.";
e.Cancel = true;
}
}
// Convert the email address to lowercase.
String emailValue = e.NewValues["EmailAddress"].ToString();
e.NewValues["EmailAddress"] = emailValue.ToLower();
}
//</Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView.ItemUpdating Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView.ItemUpdating Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemUpdating="ContactsListView_ItemUpdating"
runat="server">
<LayoutTemplate>
<table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td valign="top">
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
<td>
<asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr style="background-color:#ADD8E6">
<td valign="top">
<asp:Label runat="server" ID="FirstNameLabel"
AssociatedControlID="FirstNameTextBox" Text="First Name"/>
<asp:TextBox ID="FirstNameTextBox" runat="server" Width="200px"
Text='<%#Bind("FirstName") %>' MaxLength="50" /><br />
<asp:Label runat="server" ID="LastNameLabel"
AssociatedControlID="LastNameTextBox" Text="Last Name" />
<asp:TextBox ID="LastNameTextBox" runat="server" Width="200px"
Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
<asp:Label runat="server" ID="EmailLabel"
AssociatedControlID="EmailTextBox" Text="Email" />
<asp:TextBox ID="EmailTextBox" runat="server" Width="200px"
Text='<%#Bind("EmailAddress") %>' MaxLength="50" />
</td>
<td colspan="2" valign="top">
<asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</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="ContactsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress]
FROM Person.Contact"
UpdateCommand="UPDATE Person.Contact
Set [FirstName] = @FirstName, [LastName] = @LastName, [EmailAddress] = @EmailAddress
WHERE [ContactID] = @ContactID">
</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
'<Snippet2>
Sub ContactsListView_ItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs)
' Cancel the update operation if any of the fields is empty
' or null.
For Each de As DictionaryEntry In e.NewValues
' Check if the value is null or empty
If de.Value Is Nothing OrElse de.Value.ToString().Trim().Length = 0 Then
Message.Text = "Cannot set a field to an empty value."
e.Cancel = True
End If
Next
' Convert the email address to lowercase.
Dim emailValue As String = e.NewValues("EmailAddress").ToString()
e.NewValues("EmailAddress") = emailValue.ToLower()
End Sub
'</Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView.ItemUpdating Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView.ItemUpdating Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemUpdating="ContactsListView_ItemUpdating"
runat="server">
<LayoutTemplate>
<table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td valign="top">
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
<td>
<asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr style="background-color:#ADD8E6">
<td valign="top">
<asp:Label runat="server" ID="FirstNameLabel"
AssociatedControlID="FirstNameTextBox" Text="First Name"/>
<asp:TextBox ID="FirstNameTextBox" runat="server" Width="200px"
Text='<%#Bind("FirstName") %>' MaxLength="50" /><br />
<asp:Label runat="server" ID="LastNameLabel"
AssociatedControlID="LastNameTextBox" Text="Last Name" />
<asp:TextBox ID="LastNameTextBox" runat="server" Width="200px"
Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
<asp:Label runat="server" ID="EmailLabel"
AssociatedControlID="EmailTextBox" Text="Email" />
<asp:TextBox ID="EmailTextBox" runat="server" Width="200px"
Text='<%#Bind("EmailAddress") %>' MaxLength="50" />
</td>
<td colspan="2" valign="top">
<asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</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="ContactsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress]
FROM Person.Contact"
UpdateCommand="UPDATE Person.Contact
Set [FirstName] = @FirstName, [LastName] = @LastName, [EmailAddress] = @EmailAddress
WHERE [ContactID] = @ContactID">
</asp:SqlDataSource>
</form>
</body>
</html>
備註
當ListView呼叫 方法或按兩下專案的 [更新] 按鈕時,但在控件更新專案之前ListView,控件會引發 ItemUpdating 事件UpdateItem。 ([更新] 按鈕是屬性設定為 「Update」 的按鈕 CommandName
。) 這可讓您提供事件處理方法,在發生此事件時執行自定義例程,例如取消更新作業。
ListViewUpdateEventArgs對象會傳遞至事件處理方法。 此物件可讓您判斷目前專案的索引,並指定應該取消更新作業。 若要取消更新作業,請將 Cancel 物件的屬性 ListViewUpdateEventArgs 設定為 true
。 您也可以在值傳遞至數據源之前,先使用 Keys、 OldValues和 NewValues 集合。 使用這些集合的一般方式是先驗證或 HTML 編碼使用者提供的值,再將這些值儲存在數據源中。 這有助於防止腳本插入式攻擊。
如需 ListViewUpdateEventArgs 類別之執行個體的初始屬性值清單,請參閱 ListViewSelectEventArgs 建構函式。
建構函式
ListViewUpdateEventArgs(Int32) |
初始化 ListViewUpdateEventArgs 類別的新執行個體。 |
屬性
Cancel |
取得或設定值,這個值表示是否應該取消事件。 (繼承來源 CancelEventArgs) |
ItemIndex |
取得要更新之資料項目的索引。 |
Keys |
取得表示要更新之項目中索引鍵的欄位名稱/值組的字典。 |
NewValues |
取得包含要更新之項目修訂值的字典。 |
OldValues |
取得包含要更新之項目原始值的字典。 |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |