Sqldatasource updating event
NET 2.0 introduced a number of new Web controls designed for accessing and modifying data. NET 2.0's data source controls make it possible to retrieve, insert, update, and delete data without having to write the tedious dedata access code that was so commonplace in ASP. Previous installments of this article series have looked at returning, inserting, and deleting data using the Sql Data Source control. These controls can work in unison with the data source controls to facilitate inserting, updating, and deleting data.
These controls allow page developers to declaratively access and modify data without writing any code to perform the data access. In this article we will examine basics of using the Sql Data Source and Grid View controls to update records from a database.
With the Sql Data Source control's , and so forth). If you are configuring this information via the Update Query property from the Properties window, then your screen should look like the following: Select Command="SELECT [Product ID], [Product Name], [Products].[Category ID], [Category Name], [Unit Price], [Discontinued] FROM [Products] INNER JOIN [Categories] ON [Products].[Category ID] = [Categories].[Category ID] ORDER BY [Product Name]" Update Command="UPDATE [Products] SET [Product Name] = @Product Name, [Category ID] = @Category ID, [Unit Price] = @Unit Price, [Discontinued] = @Discontinued WHERE [Product ID] = @Product ID" Note that each parameter in the Update Parameters collection is a Parameter object.
Recall that when using manually-created Web Forms in the inserting and deleting scenarios, we would use Control Parameter objects that referenced the particular Web control that held the parameter's value.
When we examined inserting data and deleting data we looked at doing so both through manually-created Web Forms and through data Web controls, like the Grid View or Details View.
In the manually-created Web Forms example for inserting, for example, we added Text Boxes, Drop Down Lists, and other Web controls to collect the values for the new record.
Once they've selected this record, you need to show it's existing values in editable fields (textboxes, cehckboxes, and so on), let the user make any modifications, and then save them.
We can easily create the Web controls necessary to facilitate an editing interface and it is simple enough to use a Sql Data Source control with appropriate Update Parameters to execute the appropriate statement with values based on the Web controls' values.
The Grid View control makes displaying, editing, and deleting data as easy as pointing and clicking.
The Edit button disappears and in its place Update and Cancel buttons appear.
Furthermore, the columns in the Grid View become editable, too.
The screenshot below shows the Grid View when one of its record's Edit buttons has been clicked.
There has been a postback and the selected row is now editable. On postback, the Grid View takes the values from the input controls in its editing interface and automatcially populates those into the corresponding Update Parameters.