Working with data using the ASP.net 2.0 DetailsView control
Source: Internet
Author: User
Asp.net| Control | Data DetailsView control
In ASP.net 1.1, the operation of a single record needs to be defined and expanded, but at 2.0 we have the Detaillsview control, which makes it simple and straightforward. This control can bind to any data source and use its data manipulation command set.
The DetailsView control needs to be bound to the data source control first. The data source control is responsible for connecting the stored data and returning the selected data. Binding the DetailsView control to the data is so simple that you can simply set the DataSourceID property declaration. You can also take advantage of code options.
Specific actions and data source types are involved, but as long as the underlying data source allows and the corresponding SQL commands are defined, the DetailsView control can page, update, insert, and delete data from its underlying data source. The Detaillsview control and the GridView control have the same characteristics and are easy to use without having to set up your own code.
Declaration syntax (Declarative Syntax)
You can use the DetailsView control by using the syntax of listing A. (You will notice that many of the properties used by the code are the same as those supported by the GridView control.) )
The number of detailed code options for the control is numerous, and the specific declaration syntax can be found in the samples given by Microsoft. You can control the page options (paging option) through the pagersettings element, and in addition to controlling the display modes for different areas of the control, such as the end of the page, the header, the single row, and so on. A simple example of listing B demonstrates the use of the DetailsView control.
The sample code binds the DetailsView control with the data source contained in the Web page (back-end SQL Server). The DetailsView control contains page links (allowpaging= "true"), but deletion (delete), edit, and update (update) buttons are prohibited.
Through the page link, you can view the database record by page. By default, page links appear directly below the Update button as digital links. You can change the display appearance of the control through numerous page options. In addition, the delete and update buttons need to use the Delete and update commands defined by the data source control to manipulate the records stored on the back-end server.
The syntax checklist just now contains the Autogeneraterows property, which allows the system to automatically display the record (true), or (false) to customize the fields that need to be displayed. The field (Fields) element can be used to define the fields that the DetailView should contain. The possible types of control fields are as follows:
BoundField field: Binds the field to a data item in the underlying data source and displays the data.
ButtonField field: Displays a button.
CheckBoxField field: Displays a check box.
CommandField field: Displays a command button.
HyperLinkField field: Displays URL links.
ImageField field: Displays the underlying data as a picture.
TemplateField field: Used to customize the display format for data elements.
A more detailed description of the field type please click here to view.
Listing C uses a field of type BoundField and optionally displays data from the SQL Server data source by specifying the user label (custom label). At the same time, the example also uses the Delete, edit, and Update button options, which are implemented by setting the properties of the following DetailsView objects.
AutoGenerateDeleteButton property: The data type of the property is a Boolean value that specifies whether the Delete button is displayed. The DeleteCommand property of the associated data connection is used to perform the deletion of the selected record.
Autogenerateinsertbutton property: The data type of the property is a Boolean value that specifies whether to display the Insert button. The InsertCommand property of the associated data connection is used to perform insert work on the new record.
AutoGenerateEditButton property: The data type of the property is a Boolean value that specifies whether the Edit button is displayed. Selecting this button turns the record into edit mode and displays an Update button to save the changes. The UpdateCommand property of the associated data connection is used to save changes to the backend data source.
Note: The EmployeeID field cannot be edited because it is the primary key (primary key) in the underlying datasheet (table) and its read-only (READONLY) tag is set to "true".
TemplateField type fields provide more control over the data fields in the DetailsView data control. With it, you can control the appearance and behavior of the field when the data is edited, viewed, or added to the new data. The following elements are used to control these behaviors.
EditItemTemplate element: The current DetailsView record controls the appearance of this field in edit mode.
InsertItemTemplate element: Determines how this field will appear when a new record is added to the underlying data source.
ItemTemplate element: Determines how the field is displayed when the user views it.
Listing d uses a TemplateField type of field to show employee employment data. When a data is edited, or a new record is added, a calendar control appears to facilitate the user in entering date-type data. Date values are displayed in a simple label control in view mode.
Easy to use
Many of the new features of ASP.net 2.0 are characterized by ease of use. This is especially true for new data controls that are combined with the DetailsView control. By doing some very simple processing, you can access and display data, and you can also support editing, deletion, and insertion without having to write code.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service