ListView.ItemUpdated 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在请求更新操作且 ListView 控件更新项之后发生。
public:
event EventHandler<System::Web::UI::WebControls::ListViewUpdatedEventArgs ^> ^ ItemUpdated;
public event EventHandler<System.Web.UI.WebControls.ListViewUpdatedEventArgs> ItemUpdated;
member this.ItemUpdated : EventHandler<System.Web.UI.WebControls.ListViewUpdatedEventArgs>
Public Custom Event ItemUpdated As EventHandler(Of ListViewUpdatedEventArgs)
事件类型
示例
以下示例演示如何为 ItemUpdated 事件添加事件处理程序。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ 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_ItemUpdated(Object sender, ListViewUpdatedEventArgs e)
{
if (e.Exception != null)
{
if (e.AffectedRows == 0)
{
e.KeepInEditMode = true;
Message.Text = "An exception occurred updating the contact. " +
"Please verify your values and try again.";
}
else
Message.Text = "An exception occurred updating the contact. " +
"Please verify the values in the recently updated item.";
e.ExceptionHandled = true;
}
}
//</Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView.ItemUpdated Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView.ItemUpdated Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemUpdated="ContactsListView_ItemUpdated"
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"
Text='<%# Bind("FirstName") %>' MaxLength="50" Width="200px" /><br />
<asp:Label runat="server" ID="LastNameLabel"
AssociatedControlID="LastNameTextBox" Text="Last Name" />
<asp:TextBox ID="LastNameTextBox" runat="server"
Text='<%# Bind("LastName") %>' MaxLength="50" Width="200px" /><br />
<asp:Label runat="server" ID="EmailLabel"
AssociatedControlID="EmailTextBox" Text="Email" />
<asp:TextBox ID="EmailTextBox" runat="server"
Text='<%# Bind("EmailAddress") %>' MaxLength="50" Width="200px" />
</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_ItemUpdated(sender As Object, e As ListViewUpdatedEventArgs)
If e.Exception IsNot Nothing Then
If e.AffectedRows = 0 Then
e.KeepInEditMode = True
Message.Text = "An exception occurred updating the contact. " & _
"Please verify your values and try again."
Else
Message.Text = "An exception occurred updating the contact. " & _
"Please verify the values in the recently updated item."
End If
e.ExceptionHandled = True
End If
End Sub
'</Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView.ItemUpdated Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView.ItemUpdated Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemUpdated="ContactsListView_ItemUpdated"
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>
<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"
Text='<%# Bind("FirstName") %>' MaxLength="50" Width="200px" /><br />
<asp:Label runat="server" ID="LastNameLabel"
AssociatedControlID="LastNameTextBox" Text="Last Name" />
<asp:TextBox ID="LastNameTextBox" runat="server"
Text='<%# Bind("LastName") %>' MaxLength="50" Width="200px" /><br />
<asp:Label runat="server" ID="EmailLabel"
AssociatedControlID="EmailTextBox" Text="Email" />
<asp:TextBox ID="EmailTextBox" runat="server"
Text='<%# Bind("EmailAddress") %>' MaxLength="50" Width="200px" />
</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>
注解
在 ItemUpdated 控件更新项后,单击项的“更新”按钮或 UpdateItem 调用 方法时 ListView ,将引发 事件。 (“更新”按钮是一个按钮控件,其 CommandName 属性设置为“Update”。) 这样就可以在发生此事件时执行自定义例程,例如检查更新操作的结果。
对象 ListViewUpdatedEventArgs 将传递给事件处理程序,这使你能够确定受影响的行数。 它还允许你确定可能发生的异常。 可以通过设置 ExceptionHandled 对象的 属性来指示是否在事件处理程序中处理了 ListViewUpdatedEventArgs 异常。
有关如何处理事件的详细信息,请参阅 处理和引发事件。