ListViewInsertEventArgs 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 ItemInserting 事件提供数据。
public ref class ListViewInsertEventArgs : System::ComponentModel::CancelEventArgs
public class ListViewInsertEventArgs : System.ComponentModel.CancelEventArgs
type ListViewInsertEventArgs = class
inherit CancelEventArgs
Public Class ListViewInsertEventArgs
Inherits CancelEventArgs
- 继承
示例
以下示例演示如何使用 ListViewInsertEventArgs 对象在数据项包含空值时取消插入操作。
重要
此示例包含一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,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_ItemInserting(Object sender, ListViewInsertEventArgs e)
{
// Iterate through the values to verify if they are not empty.
foreach (DictionaryEntry de in e.Values)
{
if (de.Value == null)
{
Message.Text = "Cannot insert an empty value.";
e.Cancel = true;
}
}
}
//</Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView.ItemInserting Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView.ItemInserting Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemInserting="ContactsListView_ItemInserting"
InsertItemPosition="LastItem"
runat="server">
<LayoutTemplate>
<table cellpadding="2" border="1" runat="server" id="tblProducts" 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>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr style="background-color:#B0C4DE">
<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" /><br />
<asp:Label runat="server" ID="LastNameLabel"
AssociatedControlID="LastNameTextBox" Text="Last Name" />
<asp:TextBox ID="LastNameTextBox" runat="server"
Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
<asp:Label runat="server" ID="EmailLabel"
AssociatedControlID="EmailTextBox" Text="Email" />
<asp:TextBox ID="EmailTextBox" runat="server"
Text='<%#Bind("EmailAddress") %>' MaxLength="50" /><br />
</td>
<td>
<asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="ContactsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress]
FROM Person.Contact"
InsertCommand="INSERT INTO Person.Contact
([FirstName], [LastName], [EmailAddress], [PasswordHash], [PasswordSalt])
Values(@FirstName, @LastName, @EmailAddress, '', '');
SELECT @ContactID = SCOPE_IDENTITY()">
<InsertParameters>
<asp:Parameter Name="FirstName" />
<asp:Parameter Name="LastName" />
<asp:Parameter Name="EmailAddress" />
<asp:Parameter Name="ContactID" Type="Int32" Direction="Output" />
</InsertParameters>
</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_ItemInserting(ByVal sender As Object, _
ByVal e As ListViewInsertEventArgs)
' Iterate through the values to verify if they are not empty.
For Each de As DictionaryEntry In e.Values
If de.Value Is Nothing Then
Message.Text = "Cannot insert an empty value."
e.Cancel = True
End If
Next
End Sub
'</Snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView.ItemInserting Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView.ItemInserting Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemInserting="ContactsListView_ItemInserting"
InsertItemPosition="LastItem"
runat="server">
<LayoutTemplate>
<table cellpadding="2" border="1" runat="server" id="tblProducts" 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>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr style="background-color:#B0C4DE">
<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" /><br />
<asp:Label runat="server" ID="LastNameLabel"
AssociatedControlID="LastNameTextBox" Text="Last Name" />
<asp:TextBox ID="LastNameTextBox" runat="server"
Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
<asp:Label runat="server" ID="EmailLabel"
AssociatedControlID="EmailTextBox" Text="Email" />
<asp:TextBox ID="EmailTextBox" runat="server"
Text='<%#Bind("EmailAddress") %>' MaxLength="50" /><br />
</td>
<td>
<asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="ContactsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress]
FROM Person.Contact"
InsertCommand="INSERT INTO Person.Contact
([FirstName], [LastName], [EmailAddress], [PasswordHash], [PasswordSalt])
Values(@FirstName, @LastName, @EmailAddress, '', '');
SELECT @ContactID = SCOPE_IDENTITY()">
<InsertParameters>
<asp:Parameter Name="FirstName" />
<asp:Parameter Name="LastName" />
<asp:Parameter Name="EmailAddress" />
<asp:Parameter Name="ContactID" Type="Int32" Direction="Output" />
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
注解
控件 ListView 在 ItemInserting 单击控件中的“插入”按钮时,但在控件插入记录之前 ListView 引发 事件。 (“插入”按钮是一个按钮,其 CommandName
属性设置为“Insert”。) 这使你可以提供一个事件处理方法,该方法在发生此事件时执行自定义例程。 例如,可以在将记录插入数据源之前验证或对记录的值进行 HTML 编码。
对象 ListViewInsertEventArgs 传递给事件处理方法。 此对象使你能够确定发送到 ListView 控件的可选命令参数的值。 可以使用 属性访问 ListViewItem 正在插入 Item 的对象。 还可以使用 Values 属性读取或修改新记录的字段值。 若要取消插入操作,请将 Cancel 属性设置为 true
。
有关 ListViewInsertEventArgs 类的实例的初始属性值列表,请参见 ListViewInsertEventArgs 构造函数。
构造函数
ListViewInsertEventArgs(ListViewItem) |
初始化 ListViewInsertEventArgs 类的新实例。 |
属性
Cancel |
获取或设置指示是否应取消事件的值。 (继承自 CancelEventArgs) |
Item |
获取一个 ListViewItem 对象,它表示要插入的数据项。 |
Values |
获取要插入的记录的值。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |