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, thecontrol executes a SQL Update statement using the changed data as parameter values. The 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 theproperty 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'sproperty to .
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 itsevent. This event allows you to perform post-query logic such as integrity checks. Similarly, the FormView control raises its event after an insert, and its 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 acontrol and a FormView control to display data. The FormView control is configured to allow the data to be modified.
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/TR/xhtml1/DTD/xhtml1-transitional.dtd"> "https://www.w3.org/1999/xhtml" > "server">