Learning Resources

Using the FormView control to insert a row

How the FormView Control Modifies Bound Data

The FormView control displays the specified templates to provide a user interface (UI) that allows users to modify the contents of a record. Each template contains the command buttons that a user can click to perform an edit or insert action. When the user clicks a command button, the FormView control redisplays the bound record with the specified edit or insert template to allow users to modify the record.

An insert or edit template typically includes an "Insert" button to allow users to display a blank record or an "Update" button to save a change. When users click the insert or update button, the FormView control passes the bound values and primary key information to the associated data source control, which performs the appropriate update. For example, the SqlDataSource control executes a SQL Update statement using the changed data as parameter values. The ObjectDataSource control calls its update method, passing the changes as part of the method call.

When the FormView control is performing an insert operation, it passes the values to be inserted in the data source using the Values dictionary collection.

The FormView control passes values to the data source for an update or delete operation in three dictionary collections: the Keys dictionary, the NewValues dictionary, and the OldValues dictionary. You can access each dictionary using the arguments passed to the FormView control's insert, update or delete events.

The Keys dictionary contains the names and values of fields that uniquely identify the record to update or delete, and always contains the original values of the key fields. To specify which fields are placed in the Keys dictionary, set the DataKeyNames property to a comma-separated list of field names that represent the primary key of your data. An array is automatically populated with the values associated with the fields specified for the DataKeyNames property.


The original primary key values for the fields specified in the DataKeyNames property are stored in view state. If your primary key values contain sensitive information, you should encrypt the view state contents by setting the page's ViewStateEncryptionMode property to Always.

The Values and NewValues dictionaries contain the current values from the controls in the record being inserted or edited in the FormView control. The OldValues dictionary contains any original values of fields except the key fields, which are included in the Keys dictionary. New values for editable key fields are contained in the NewValues dictionary.

The data source control uses the values from the Keys, Values, NewValues, and OldValues dictionaries as parameters for the insert, update, or delete command. For information about how data source control parameters are created based on the dictionaries created for bound values.

After the update is complete, the FormView control raises its ItemUpdated event. This event allows you to perform post-query logic such as integrity checks. Similarly, the FormView control raises its ItemInserted event after an insert, and its ItemDeleted event after a delete.

After the update is complete and all events have been raised, the FormView control rebinds to the data source control.


The following code example uses a GridView control and a FormView control to display data. The FormView control is configured to allow the data to be modified.

Security noteSecurity Note

This code 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. 

<%@ Page language="C#" %>

"-//W3C//DTD XHTML 1.0 Transitional//EN"

"https://www.w3.org/1999/xhtml" >
    FormView Example

"form1" runat="server">

FormView Example

"EmployeesGridView" DataSourceID="EmployeesSqlDataSource" AutoGenerateColumns="false" DataKeyNames="EmployeeID" OnSelectedIndexChanged="EmployeesGridView_OnSelectedIndexChanged" RunAt="Server"> "Navy" forecolor="White" /> "Details..." HeaderText="Show
CommandName="Select"/> "EmployeeID" HeaderText="Employee ID"/> "LastName" HeaderText="Last Name"/> "FirstName" HeaderText="First Name"/>
"top"> "EmployeeFormView" DataSourceID="EmployeeDetailsSqlDataSource" DataKeyNames="EmployeeID" Gridlines="Both" OnItemUpdated="EmployeeFormView_ItemUpdated" OnItemDeleted="EmployeeFormView_ItemDeleted" RunAt="server"> "Navy" forecolor="White"/> "White"/> "LightCyan"/>
"right">Employee ID:<%# Eval("EmployeeID") %>
"right">First Name: <%# Eval("FirstName") %>
"right">Last Name: <%# Eval("LastName") %>
"2"> "EditButton" Text="Edit" CommandName="Edit" RunAt="server"/>   "NewButton" Text="New" CommandName="New" RunAt="server"/>   "DeleteButton" Text="Delete" CommandName="Delete" RunAt="server"/>
"right">Employee ID:<%# Eval("EmployeeID") %>
"right">First Name: "EditFirstNameTextBox" Text='<%# Bind("FirstName") %>' RunAt="Server" />
"right">Last Name: "EditLastNameTextBox" Text='<%# Bind("LastName") %>' RunAt="Server" />
"2"> "UpdateButton" Text="Update" CommandName="Update" RunAt="server"/>   "CancelUpdateButton" Text="Cancel" CommandName="Cancel" RunAt="server"/>
"right">First Name: "InsertFirstNameTextBox" Text='<%# Bind("FirstName") %>' RunAt="Server" />
"right">Last Name: "InsertLastNameTextBox" Text='<%# Bind("LastName") %>' RunAt="Server" />
"2"> "InsertButton" Text="Insert" CommandName="Insert" RunAt="server"/>   "CancelInsertButton" Text="Cancel" CommandName="Cancel" RunAt="server"/>
"EmployeesSqlDataSource" selectCommand="SELECT EmployeeID, FirstName, LastName FROM Employees" connectionstring="<%$ ConnectionStrings:NorthwindConnection %>" RunAt="server"> "EmployeeDetailsSqlDataSource" SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID" InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName); SELECT @EmpID = SCOPE_IDENTITY()" UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName WHERE EmployeeID=@EmployeeID" DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID" ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" OnInserted="EmployeeDetailsSqlDataSource_OnInserted" RunAt="server"> "EmpID" Type="Int32" DefaultValue="0" /> "EmpID" Direction="Output" Type="Int32" DefaultValue="0" />