ListViewInsertEventArgs Class
Provides data for the ItemInserting event.
Assembly: System.Web.Extensions (in System.Web.Extensions.dll)
The ListView control raises the ItemInserting event when an Insert button in the control is clicked, but before the ListView control inserts the record. (An Insert button is a button whose CommandName property is set to "Insert".) This enables you to provide an event-handling method that performs a custom routine whenever this event occurs. For example, you can validate or HTML-encode the values of a record before it is inserted it in the data source.
A ListViewInsertEventArgs object is passed to the event-handling method. This object enables you to determine the value of an optional command argument that is sent to the ListView control. You can access the ListViewItem object that is being inserted by using the Item property. You can also read or modify the field values for the new record by using the Values property. To cancel the insert operation, set the Cancel property to true.
For a list of initial property values for an instance of the ListViewInsertEventArgs class, see the ListViewInsertEventArgs constructor.
The following example shows how to use the ListViewInsertEventArgs object to cancel an insert operation if the data item contains an empty value.
Security Note: |
|---|
This example contains a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see 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; } 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; } } } </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="E-mail" /> <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>
- AspNetHostingPermission
for operating in a hosted environment. Demand value: LinkDemand. Associated enumeration: AspNetHostingPermissionLevel.Minimal
- AspNetHostingPermission
for operating in a hosted environment. Demand value: InheritanceDemand. Associated enumeration: AspNetHostingPermissionLevel.Minimal
System.EventArgs
System.ComponentModel.CancelEventArgs
System.Web.UI.WebControls.ListViewInsertEventArgs
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Security Note: