FormViewInsertEventArgs Class
Provides data for the ItemInserting event.
System.EventArgs
System.ComponentModel.CancelEventArgs
System.Web.UI.WebControls.FormViewInsertEventArgs
Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
The FormViewInsertEventArgs type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | FormViewInsertEventArgs | Initializes a new instance of the FormViewInsertEventArgs class. |
| Name | Description | |
|---|---|---|
![]() | Cancel | Gets or sets a value indicating whether the event should be canceled. (Inherited from CancelEventArgs.) |
![]() | CommandArgument | Gets the command argument for the insert operation passed to the FormView control. |
![]() | Values | Gets a dictionary that contains the field name/value pairs for the record to insert. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified object is equal to the current object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
The FormView control raises the ItemInserting event when an Insert button (a button with its CommandName property set to "Insert") within the control is clicked, but before the FormView control inserts the record. This allows you to provide an event-handling method that performs a custom routine, such as HTML encoding or validating the values of a record before inserting it in the data source, whenever this event occurs.
A FormViewInsertEventArgs object is passed to the event-handling method, which allows you to determine the value of an optional command argument sent to the FormView control and to indicate that the insert operation should be canceled. To determine the value of the command argument, use the CommandArgument property. To cancel the insert operation, set the Cancel property to true. You can also read or modify the field values for the new record by using the Values property.
For more information about handling events, see Consuming Events.
For a list of initial property values for an instance of the FormViewInsertEventArgs class, see the FormViewInsertEventArgs constructor.
The following example demonstrates how to use the FormViewInsertEventArgs object passed to the event-handling method for the ItemInserting event to cancel an insert operation when the user leaves a field empty.
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 EmployeeFormView_ItemInserting(Object sender, FormViewInsertEventArgs e) { MessageLabel.Text = ""; // Iterate through the items in the Values collection // and verify that the user entered a value for each // text box displayed in the insert item template. Cancel // the insert operation if the user left a text box empty. foreach (DictionaryEntry entry in e.Values) { if (entry.Value.Equals("")) { // Use the Cancel property to cancel the // insert operation. e.Cancel = true; MessageLabel.Text += "Please enter a value for the " + entry.Key.ToString() + " field.<br/>"; } } } void EmployeeFormView_ModeChanged(Object sender, EventArgs e) { // Clear the MessageLabel Label control when the FormView // control changes modes. MessageLabel.Text = ""; } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>FormViewInsertEventArgs Example</title> </head> <body> <form id="form1" runat="server"> <h3>FormViewInsertEventArgs Example</h3> <asp:formview id="EmployeeFormView" datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" emptydatatext="No employees found." oniteminserting="EmployeeFormView_ItemInserting" onmodechanged="EmployeeFormView_ModeChanged" runat="server"> <itemtemplate> <table> <tr> <td rowspan="5"> <asp:image id="CompanyLogoImage" imageurl="~/Images/Logo.jpg" alternatetext="Company logo" runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b>Name:</b> </td> <td> <%# Eval("FirstName") %> <%# Eval("LastName") %> </td> </tr> <tr> <td> <b>Title:</b> </td> <td> <%# Eval("Title") %> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="NewButton" text="New" commandname="New" runat="server"/> </td> </tr> </table> </itemtemplate> <insertitemtemplate> <table> <tr> <td rowspan="4"> <asp:image id="CompanyLogoEditImage" imageurl="~/Images/Logo.jpg" alternatetext="Company logo" runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b><asp:Label runat="server" AssociatedControlID="FirstNameInsertTextBox" Text="Name" />:</b> </td> <td> <asp:textbox id="FirstNameInsertTextBox" text='<%# Bind("FirstName") %>' runat="server"/> <asp:textbox id="LastNameInsertTextBox" text='<%# Bind("LastName") %>' runat="server"/> </td> </tr> <tr> <td> <b><asp:Label runat="server" AssociatedControlID="TitleInsertTextBox" Text="Title" />:</b> </td> <td> <asp:textbox id="TitleInsertTextBox" text='<%# Bind("Title") %>' runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="InsertButton" text="Insert" commandname="Insert" runat="server"/> <asp:linkbutton id="CancelButton" text="Cancel" commandname="Cancel" runat="server"/> </td> </tr> </table> </insertitemtemplate> </asp:formview> <br/><br/> <asp:label id="MessageLabel" forecolor="Red" runat="server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="EmployeeSource" selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]" insertcommand="Insert Into [Employees] ([LastName], [FirstName], [Title]) VALUES (@LastName, @FirstName, @Title)" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
