asp.net Web controls that typically support data binding, such as dorpdownlist controls, CheckBoxList controls, and so on, all contain five properties: DataSource, Datamember,datatextfield, DataValueField and Datatextformatstring. DataSource is used to get the data source, that is, to get the collection containing the data; the other four properties are used to get a field value from the data source. Typically, however, you want to bind multiple field values from these data sources together and assign them to one of these properties, which cannot be implemented directly. Here are two ways to implement this functionality.
The above limitation is that you cannot bind multiple field values together and assign them to one of these four properties, but the SQL statement has no such limitation at all. So, our first approach is to use the SQL statement to bind two or more field values in the source table, using the AS clause to pass to another field, which implements a field that contains more than one field value. Since a field now contains the values of more than one field, the four properties that we assign to the Web control do not have to be considered more. We can refer to the following statement to see the beauty of this approach:
strcmd= "Select Employeeid,lastname +", ' +firstname as ' employeename ' from Employees ';
This method is very concise and one of the most direct methods, but there is a problem of inefficiency, and it is possible to produce duplicate data. Here's another way we can populate a dataset with a source table, the dataset's data is then placed in a DataTable, and then a new column is created for the table, consisting of two or more fields in the source table, so that we also implement the binding of multiple field values. The following statement illustrates the implementation of this method:
The disadvantage of the latter approach is the overhead of creating a new column. However, this method is well organized and is not likely to produce duplicate data, which is recommended.
To illustrate both methods, the following code implements the two methods together:
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.