How to: Allow Users to Edit Items in DataList Web Server Controls
You can allow users to edit individual items in theWeb server control. When an individual item is set to edit mode, the values that can be changed are usually displayed in text boxes or other controls in which users can make their changes.
To allow users to edit items in a DataList control
Set the DataList control'sproperty to the name of the field in the data that contains the primary key.
Create an(and, if you are using it, an ), and then add a Web server control to it. Set the property for this button to edit.
You can use aor an control in any step that calls for a Button Web server control.
Create anfor the DataList control that includes the following:
Controls for all of the values that users can change. For example, includecontrols for all character and numeric data. Use the declarative method to specify which field each control is bound to, as in this example:
This example has 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.
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("ProductName") %>' />
A Button control with itsproperty set to "Update" and its CommandName property set to update (case-sensitive).
A Button control with its Text property set to "Cancel" and its CommandName property set to cancel.
The Update button will allow users to specify that they are finished editing and will save any changes. The Cancel button will allow them to quit editing without saving changes.
Write code to perform the following tasks:
Handle the DataList control'sevent that sets the DataList control's property to the index value of the item to put into edit mode. The index of the item that the user clicked is available via the Item object's property. Then call the control's method.
Handle the DataList control'sevent that sets the DataList control's EditItemIndex property to -1 and then calls the control's DataBind method.
Handle the DataList control'sevent. In the code, extract the values from the controls in the current item and pass them to the data source control for an update operation. The exact code you use depends on the type of data source control that you are working with.
The following code example shows an ASP.NET page that uses a DataList control and acontrol to display information from the Categories table in the Northwind database. Users can edit the items.
To update data, you need the primary key of the record that is being updated. You can get that value from the DataKeyField property, which contains an array of keys.
To get the value of a specific control in the item, use themethod of the Item event-argument object.
The values that you set in the SqlDataSource1.UpdateParameters dictionary must match the names that you set in the UpdateParameters element.
Compiling the Code
The code requires that you have a connection string named
NorthwindConnectionString. The database you connect to is assumed to have a table named Categories with the fields CategoryID, CategoryName, and Description.
The account under which the page connects to the database must have permission to update the Category table.
The code in the example does not perform the following tasks that you would normally do in a production environment:
The code does not include error checking to be sure that the FindControl method returns a valid control. For more robust code, make sure that the value returned by the FindControl method is not a null reference (Nothing in Visual Basic).
The code does not check whether the update was successful.
User input in a Web Forms page can include potentially malicious client script. By default, the Web Forms page validates that user input does not include script or HTML elements. For more information, see.