The
Enabling the Built-In Editing Functionality
You can enable the built-in editing functionality of the DetailsView control by setting one or more of the
How the DetailsView Control Modifies Bound Data
The DetailsView control provides a user interface (UI) that allows users to modify the contents of a bound record. Typically, an editable view displays an additional row that contains command buttons with the text Edit, Insert, and Delete. By default, the row is added to the bottom of the DetailsView control.
When the user clicks a command button, the DetailsView control redisplays the row with controls that allow users to modify the contents of the row. The edit button is replaced with buttons that allow the user to save changes or cancel editing a row. The DetailsView control uses text boxes to display data in a
When the DetailsView control is performing an insert operation, it passes the values to be inserted in the data source using the Values dictionary collection.
For an update or delete operation, the DetailsView control passes values to the data source in three dictionary collections: the Keys dictionary, the NewValues dictionary, and the OldValues dictionary. You can access each dictionary using the event arguments passed to the insert, update, or delete events raised by the DetailsView control.
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 before the record was edited. To specify which fields are placed in the Keys dictionary, set the
![]() |
---|
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 |
The Values and NewValues dictionaries contain the current values from the input controls in the record being inserted or edited, respectively. The OldValues dictionary contains any original values of fields except the key fields, which are included in the Keys dictionary. New values for key fields are included in the NewValues dictionary.
The data source control uses the values from the Keys, Values, NewValues, and OldValues dictionaries as parameters for its insert, update, or delete command. For information on how data source control parameters are created based on the dictionaries created for bound values, see How a Data Source Control Creates Parameters for Data-bound Fields.
After an update, the DetailsView control raises its
After the update is complete and all events have been raised, the DetailsView control rebinds to the data source control to display the updated data.
Customizing the Editing User Interface in the DetailsView Control
By default, the DetailsView control automatically generates a row for each bound field from the data source. You can customize which fields are bound to the DetailsView control by setting the AutoGenerateRows property to false and specifying a BoundField control for each field that you want displayed in the DetailsView control.
To customize how command buttons are displayed, you can set the DetailsView control's AutoGenerateEditButton property to false. You can then add individual
You can specify whether a bound field is editable using the
Similarly, you can specify whether a value can be inserted for a bound field using the
Example
The following code example uses a
Visual BasicВ | ![]() |
---|---|
<%@ Page language="VB" %> <script RunAt="server"> Sub EmployeesDropDownList_OnSelectedIndexChanged(sender As Object, e As EventArgs) EmployeeDetailsView.DataBind() End Sub Sub EmployeeDetailsView_ItemUpdated(sender As Object, e As DetailsViewUpdatedEventArgs) EmployeesDropDownList.DataBind() EmployeesDropDownList.SelectedValue = e.Keys("EmployeeID").ToString() EmployeeDetailsView.DataBind() End Sub Sub EmployeeDetailsView_ItemDeleted(sender As Object, e As DetailsViewDeletedEventArgs) EmployeesDropDownList.DataBind() End Sub Sub EmployeeDetailsSqlDataSource_OnInserted(sender As Object, e As SqlDataSourceStatusEventArgs) Dim command As System.Data.Common.DbCommand = e.Command EmployeesDropDownList.DataBind() EmployeesDropDownList.SelectedValue = _ command.Parameters("@EmpID").Value.ToString() EmployeeDetailsView.DataBind() End Sub </script> <html> <body> <form RunAt="server"> <h3>Northwind Employees</h3> <table cellspacing="10"> <tr> <td valign="top"> <asp:DropDownList ID="EmployeesDropDownList" DataSourceID="EmployeesSqlDataSource" DataValueField="EmployeeID" DataTextField="FullName" AutoPostBack="True" Size="10" OnSelectedIndexChanged="EmployeesDropDownList_OnSelectedIndexChanged" RunAt="Server" /> </td> <td valign="top"> <asp:DetailsView ID="EmployeeDetailsView" DataSourceID="EmployeeDetailsSqlDataSource" AutoGenerateRows="false" AutoGenerateInsertbutton="true" AutoGenerateEditbutton="true" AutoGenerateDeletebutton="true" DataKeyNames="EmployeeID" Gridlines="Both" OnItemUpdated="EmployeeDetailsView_ItemUpdated" OnItemDeleted="EmployeeDetailsView_ItemDeleted" RunAt="server"> <HeaderStyle backcolor="Navy" forecolor="White"/> <RowStyle backcolor="White"/> <AlternatingRowStyle backcolor="LightGray"/> <EditRowStyle backcolor="LightCyan"/> <Fields> <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" InsertVisible="False" ReadOnly="true"/> <asp:BoundField DataField="FirstName" HeaderText="First Name"/> <asp:BoundField DataField="LastName" HeaderText="Last Name"/> <asp:BoundField DataField="Address" HeaderText="Address"/> <asp:BoundField DataField="City" HeaderText="City"/> <asp:BoundField DataField="Region" HeaderText="Region"/> <asp:BoundField DataField="PostalCode" HeaderText="Postal Code"/> </Fields> </asp:DetailsView> </td> </tr> </table> <asp:SqlDataSource ID="EmployeesSqlDataSource" SelectCommand="SELECT EmployeeID, LastName + ', ' + FirstName AS FullName FROM Employees" Connectionstring="<%$ ConnectionStrings:NorthwindConnection %>" RunAt="server"> </asp:SqlDataSource> <asp:SqlDataSource ID="EmployeeDetailsSqlDataSource" SelectCommand="SELECT EmployeeID, LastName, FirstName, Address, City, Region, PostalCode FROM Employees WHERE EmployeeID = @EmpID" InsertCommand="INSERT INTO Employees(LastName, FirstName, Address, City, Region, PostalCode) VALUES (@LastName, @FirstName, @Address, @City, @Region, @PostalCode); SELECT @EmpID = SCOPE_IDENTITY()" UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName, Address=@Address, City=@City, Region=@Region, PostalCode=@PostalCode WHERE EmployeeID=@EmployeeID" DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID" ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" OnInserted="EmployeeDetailsSqlDataSource_OnInserted" RunAt="server"> <SelectParameters> <asp:ControlParameter ControlID="EmployeesDropDownList" PropertyName="SelectedValue" Name="EmpID" Type="Int32" DefaultValue="0" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="LastName" Type="String" /> <asp:Parameter Name="FirstName" Type="String" /> <asp:Parameter Name="Address" Type="String" /> <asp:Parameter Name="City" Type="String" /> <asp:Parameter Name="Region" Type="String" /> <asp:Parameter Name="PostalCode" Type="String" /> <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="LastName" Type="String" /> <asp:Parameter Name="FirstName" Type="String" /> <asp:Parameter Name="Address" Type="String" /> <asp:Parameter Name="City" Type="String" /> <asp:Parameter Name="Region" Type="String" /> <asp:Parameter Name="PostalCode" Type="String" /> <asp:Parameter Name="EmployeeID" Type="Int32" DefaultValue="0" /> </UpdateParameters> <DeleteParameters> <asp:Parameter Name="EmployeeID" Type="Int32" DefaultValue="0" /> </DeleteParameters> </asp:SqlDataSource> </form> </body> </html> |
C#В | ![]() |
---|---|
<%@ Page language="C#" %> <script RunAt="server"> void EmployeesDropDownList_OnSelectedIndexChanged(Object sender, EventArgs e) { EmployeeDetailsView.DataBind(); } void EmployeeDetailsView_ItemUpdated(Object sender, DetailsViewUpdatedEventArgs e) { EmployeesDropDownList.DataBind(); EmployeesDropDownList.SelectedValue = e.Keys["EmployeeID"].ToString(); EmployeeDetailsView.DataBind(); } void EmployeeDetailsView_ItemDeleted(Object sender, DetailsViewDeletedEventArgs e) { EmployeesDropDownList.DataBind(); } void EmployeeDetailsSqlDataSource_OnInserted(Object sender, SqlDataSourceStatusEventArgs e) { System.Data.Common.DbCommand command = e.Command; EmployeesDropDownList.DataBind(); EmployeesDropDownList.SelectedValue = command.Parameters["@EmpID"].Value.ToString(); EmployeeDetailsView.DataBind(); } </script> <html> <body> <form RunAt="server"> <h3>Northwind Employees</h3> <table cellspacing="10"> <tr> <td valign="top"> <asp:DropDownList ID="EmployeesDropDownList" DataSourceID="EmployeesSqlDataSource" DataValueField="EmployeeID" DataTextField="FullName" AutoPostBack="True" Size="10" OnSelectedIndexChanged="EmployeesDropDownList_OnSelectedIndexChanged" RunAt="Server" /> </td> <td valign="top"> <asp:DetailsView ID="EmployeeDetailsView" DataSourceID="EmployeeDetailsSqlDataSource" AutoGenerateRows="false" AutoGenerateInsertbutton="true" AutoGenerateEditbutton="true" AutoGenerateDeletebutton="true" DataKeyNames="EmployeeID" Gridlines="Both" OnItemUpdated="EmployeeDetailsView_ItemUpdated" OnItemDeleted="EmployeeDetailsView_ItemDeleted" RunAt="server"> <HeaderStyle backcolor="Navy" forecolor="White"/> <RowStyle backcolor="White"/> <AlternatingRowStyle backcolor="LightGray"/> <EditRowStyle backcolor="LightCyan"/> <Fields> <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" InsertVisible="False" ReadOnly="true"/> <asp:BoundField DataField="FirstName" HeaderText="First Name"/> <asp:BoundField DataField="LastName" HeaderText="Last Name"/> <asp:BoundField DataField="Address" HeaderText="Address"/> <asp:BoundField DataField="City" HeaderText="City"/> <asp:BoundField DataField="Region" HeaderText="Region"/> <asp:BoundField DataField="PostalCode" HeaderText="Postal Code"/> </Fields> </asp:DetailsView> </td> </tr> </table> <asp:SqlDataSource ID="EmployeesSqlDataSource" SelectCommand="SELECT EmployeeID, LastName + ', ' + FirstName AS FullName FROM Employees" Connectionstring="<%$ ConnectionStrings:NorthwindConnection %>" RunAt="server"> </asp:SqlDataSource> <asp:SqlDataSource ID="EmployeeDetailsSqlDataSource" SelectCommand="SELECT EmployeeID, LastName, FirstName, Address, City, Region, PostalCode FROM Employees WHERE EmployeeID = @EmpID" InsertCommand="INSERT INTO Employees(LastName, FirstName, Address, City, Region, PostalCode) VALUES (@LastName, @FirstName, @Address, @City, @Region, @PostalCode); SELECT @EmpID = SCOPE_IDENTITY()" UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName, Address=@Address, City=@City, Region=@Region, PostalCode=@PostalCode WHERE EmployeeID=@EmployeeID" DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID" ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" OnInserted="EmployeeDetailsSqlDataSource_OnInserted" RunAt="server"> <SelectParameters> <asp:ControlParameter ControlID="EmployeesDropDownList" PropertyName="SelectedValue" Name="EmpID" Type="Int32" DefaultValue="0" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="LastName" Type="String" /> <asp:Parameter Name="FirstName" Type="String" /> <asp:Parameter Name="Address" Type="String" /> <asp:Parameter Name="City" Type="String" /> <asp:Parameter Name="Region" Type="String" /> <asp:Parameter Name="PostalCode" Type="String" /> <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="LastName" Type="String" /> <asp:Parameter Name="FirstName" Type="String" /> <asp:Parameter Name="Address" Type="String" /> <asp:Parameter Name="City" Type="String" /> <asp:Parameter Name="Region" Type="String" /> <asp:Parameter Name="PostalCode" Type="String" /> <asp:Parameter Name="EmployeeID" Type="Int32" DefaultValue="0" /> </UpdateParameters> <DeleteParameters> <asp:Parameter Name="EmployeeID" Type="Int32" DefaultValue="0" /> </DeleteParameters> </asp:SqlDataSource> </form> </body> </html> |