Detailsview Commandfield Update

Posted on  by admin

Jul 30, 2012 Hello, I am having an issue getting my detailsview to update a row when 'update' is clicked. One thing I discovered that may. How to use the DetailsView and FormView. CommandField A field that contains. New Places the DetailsView control in Insert mode. Update Displayed only in.

  1. Give More Feedback

The DetailsView control is used to display a single record from a data source in a table, where each field of the record is displayed in a row of the table. It can be used in combination with a control for master-detail scenarios. The DetailsView control supports the following features:. Binding to data source controls, such as. Built-in inserting capabilities.

Built-in updating and deleting capabilities. Built-in paging capabilities. Programmatic access to the DetailsView object model to dynamically set properties, handle events, and so on. Customizable appearance through themes and styles. Each data row in the DetailsView control is created by declaring a field control. Different row field types determine the behavior of the rows in the control.

Field controls derive from. The following table lists the different row field types that can be used. Column field type Description Displays the value of a field in a data source as text. Displays a command button in the DetailsView control.

This allows you to display a row with a custom button control, such as an Add or a Remove button. Displays a check box in the DetailsView control. This row field type is commonly used to display fields with a Boolean value. Displays built-in command buttons to perform edit, insert, or delete operations in the DetailsView control. Displays the value of a field in a data source as a hyperlink. This row field type allows you to bind a second field to the hyperlink's URL.

Displays an image in the DetailsView control. Displays user-defined content for a row in the DetailsView control according to a specified template. This row field type allows you to create a custom row field. By default, the property is set to true, which automatically generates a bound row field object for each field of a bindable type in the data source.

Valid bindable types are, and the set of primitive types. Each field is then displayed in a row as text, in the order in which each field appears in the data source. Automatically generating the rows provides a quick and easy way to display every field in the record. However, to make use of the DetailsView control's advanced capabilities you must explicitly declare the row fields to include in the DetailsView control. To declare the row fields, first set the property to false. Next, add opening and closing tags between the opening and closing tags of the DetailsView control. Finally, list the row fields that you want to include between the opening and closing tags.

More

The row fields specified are added to the collection in the order listed. The collection allows you to programmatically manage the row fields in the DetailsView control. The DetailsView control can be bound to a data source control (such as the control or control) or to any data source collection that implements the interface, such as, or other collection types.

Use one of the following methods to bind the DetailsView control to the appropriate data source type:. To bind to a data source control, set the property of the DetailsView control to the value of the data source control. The DetailsView control automatically binds to the specified data source control. This is the preferred method to bind to data. To bind to a data source that implements the interface, programmatically set the property of the DetailsView control to the data source and then call the method. For more information on data binding, see.

Note The DetailsView control can provide support for update, delete, insert, and paging operations with other types of data sources; however, you must provide the implementation for these operations in an appropriate event handler. For more information, see, and. The DetailsView control can automatically add a row field with an Edit, Delete, or New button by setting the, or properties to true, respectively. Unlike the Delete button (which deletes the selected record immediately), when the Edit or New button is clicked, the DetailsView control goes into edit or insert mode, respectively.

In edit mode, the Edit button is replaced with an Update and a Cancel button. Input controls that are appropriate for the field's data type (such as a or a control) are displayed with a field's value for the user to modify.

Clicking the Update button updates the record in the data source, while clicking the Cancel button abandons any changes. Likewise, in insert mode, the New button is replaced with an Insert and a Cancel button, and empty input controls are displayed for the user to enter the values for the new record. Note You can also manually define the update, delete, and insert command buttons in a, or row field.

The DetailsView control recognizes buttons with the property set to 'Edit', 'Update', 'Delete', 'New', 'Insert', or 'Cancel'; however, you still must provide the functionality yourself. For more information, see, and. The DetailsView control provides a paging feature, which allows the user to navigate to other records in the data source. When enabled, page navigation controls are displayed in a pager row. To enable paging, set the property to true.

The pager row can be customized using the and properties. You can customize the appearance of the DetailsView control by setting the style properties for different parts of the control. The following table lists the style properties that you can set. Style property Description The style settings for the alternating data rows in the DetailsView control. When this property is set, the data rows are displayed in bands, alternating between the settings and the settings. The style settings for the row that contains the built-in command buttons. The style settings for the data rows when the DetailsView control is in edit mode.

The style settings for the empty data row that are displayed in the DetailsView control when the data source does not contain any records. The style settings for the footer row. The style settings for the header row. The style settings for the data rows when the DetailsView control is in insert mode.

The style settings for the pager row. The style settings for the data rows in the DetailsView control. When the property is also set, the data rows are displayed alternating between the settings and the settings. The style settings for the header column. Angelina muniz no inseto do amor. The DetailsView control provides several events that you can program against. This allows you to run a custom routine whenever an event occurs.

The following table lists the events supported by the DetailsView control. The DetailsView control also inherits these events from its base classes:, and.

Event Description Occurs when a button is clicked in the DetailsView control. Occurs after all objects are created in the DetailsView control. This event is often used to modify the values of a record before it is displayed. Occurs when a Delete button is clicked, but after the DetailsView control deletes the record from the data source.

This event is often used to check the results of the delete operation. Occurs when a Delete button is clicked, but before the DetailsView control deletes the record from the data source. This event is often used to cancel the delete operation. Occurs when an Insert button is clicked, but after the DetailsView control inserts the record.

This event is often used to check the results of the insert operation. Occurs when an Insert button is clicked, but before the DetailsView control inserts the record. This event is often used to cancel the insert operation. Occurs when an Update button is clicked, but after the DetailsView control updates the row. This event is often used to check the results of the update operation. Occurs when an Update button is clicked, but before the DetailsView control updates the record.

This event is often used to cancel the update operation. Occurs after the DetailsView control changes modes (edit, insert, or read-only mode). This event is often used to perform a task when the DetailsView control changes modes. Occurs before the DetailsView control changes modes (edit, insert, or read-only mode). This event is often used to cancel a mode change. Occurs when one of the pager buttons is clicked, but after the DetailsView control handles the paging operation. This event is commonly used when you need to perform a task after the user navigates to a different record in the control.

Occurs when one of the pager buttons is clicked, but before the DetailsView control handles the paging operation. This event is often used to cancel the paging operation. <%@ Page Language= 'C#'%

<%@ Page Language= 'C#'%

To work with records, first we need to connect DetailsView control with some data source. In this example, I will use SqlDataSource control to bind DetailsView to SQl Server database. Here is a markup code for this SqlDataSource control: This data source contains commands for all four basic operations: Select, Update, Insert and Delete. If you only need to show record in read-only mode, your data source control needs only Select command. After we created SqlDataSource control, we need to associate it with DetailsView control. You can do that in design view, like on next image.

Also, you can do this if you set DataSourceID property of DetailsView control to the name of your data source control. To start an example, you need a database. SqlDataSource control above uses a database with Products and Categories tables. You can create these tables in your database with SQL code like this: For table Products CREATE TABLE Products ( ID int IDENTITY (1,1 ) NOT NULL, ProductName nvarchar (50 ) NOT NULL, Description nvarchar (250 ) NOT NULL, Image nvarchar (1000 ) NOT NULL, Category int NOT NULL, ProductURL nvarchar (1000 ) NOT NULL ) And, very simple table Categories CREATE TABLE Categories ( ID int IDENTITY (1,1 ) NOT NULL, CategoryName nvarchar (50 ) ) Fill tables with some data and run the example to see how it works.

Give

Of course, you can use your own existing table, just adopt SqlDataSource commands to suit your table structure. Run the example and DetailsView control will show first record, similar to the next image. This action will produce markup code like this: Before using an example you need to set DataKeyNames property of DetailsView control to the name of table primary key column. In our case table primary key column is named 'ProductID'. Now, you can start an example again and joy in new features. Test paging, updating, deleting and inserting a new record to see how it works.

See more on MSDN

You'll see new buttons Edit, Delete and New link buttons. Note that we did all this without writing single line of C# or VB.NET code!

Output should look similar to image bellow. How to provide better layout with DetailsView control Now, the example is completely functional, although it looks ugly. For example, instead of showing an URL of image, it would be better to show image itself, instead of product url it will be better to show clickable link etc.By default, DetailsView control has AutoGenerateRows property set to true. That means that all fields will be generated at run time, depending of what fields your data source contains. This is fast solution, but if you want more control over the way how fields will be presented, you need to set AutoGenerateRows property to False. Now you must specify needed fields manually. DetailsView control has seven types of fields, every with different purpose: BoundField - This is default field, used when AutoGenerateRows is true.

This layout looks definitely more user friendly. ImageField is used to show an image, primary key is presented as a read-only field and used TemplateField to enable advanced layout in product name and category selection. TemplateField is a little harder to use compared to other types of fields, but it allows almost unlimited formatting options. Practically, you can place complete all table columns in one TemplateField. TemplateField contains ItemTemplate, EditItemTemplate and InsertItemTemplate sub tags used to specify different layouts for showing and editing of existing records or adding a new record. In ItemTemplate Eval method is used since Bind provides one direction read-only binding. For EditItemTemplate and InsertItemTemplate Bind method is used.

Bind method enables binding in two directions. Selection of category is presented in different layouts: as simple text in read-only mode and with DropDownList control in EditItemTemplate and InsertItemTemplate, so users can easily select category from offered category names in DropDownList, instead of writing of category ID to TextBox control. DetailsView control layout when updating an existing record Adding Header and Footer to get more professional look To add Header and Footer elements to DetailsView control, use HeaderTemplate and FooterTemplate sub tags.

Give More Feedback

Very simple imlementation of header and footer could look like this: My favorite products Copyright © 2009, All rights reserved Adding styles in DetailsView control The easiest way to add professional formatting to DetailsView control is to use AutoFormat option. Click on AutoFormat. Link button on DetailsView tasks or in Property window and choose one of few predefined styles. I'll choose Colorful auto format that will produce output like in next image. If AutoFormat option can't satisfy your requirements, you can set styles of each element separately.