O'Reilly logo

ASP.NET 2.0 Cookbook, 2nd Edition by Geoffrey T. LeBlond, Michael A Kittel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

2.13. Editing Data Within a DataGrid

Problem

You want to allow the user to edit the data within the table displayed by a DataGrid.

Solution

Add an EditCommandColumn column type to the DataGrid control’s display to enable editing of the data fields of each record. A typical example of normal display mode output is shown in Figure 2-14, and an example of edit mode output is shown in Figure 2-15. Examples 2-36, 2-37 through 2-38 show the .aspx and code-behind files for the application that produces this result.

DataGrid with editing—normal mode

Figure 2-14. DataGrid with editing—normal mode

DataGrid with editing—row edit mode

Figure 2-15. DataGrid with editing—row edit mode

Discussion

This recipe uses the built-in editing facilities of the DataGrid control, in particular the EditCommandColumn column type, which provides Edit command buttons for editing data items in each row of a DataGrid. The EditText, CancelText, and UpdateText properties define the text to be output for the Edit command button’s Edit, Cancel, and Update hyperlinks, respectively.

	<asp:EditCommandColumn ButtonType="LinkButton"
							  EditText="Edit"
							  CancelText="Cancel"
							  UpdateText="Update" />

The ButtonType attribute defines the type of button to output. You can specify LinkButton, which provides hyperlinked text, or PushButton, which outputs an HTML button.

Tip

The Edit command button’s EditText, CancelText ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required