If you go back to the age of Visual Basic, data binding (binding) is an abandoned clumsy programming technique. DAO's overly simple and slow controls make data binding a bad name, and many experienced developers don't use it at all. The prevailing view was that "real programmers don't eat waffles, and they definitely don't use data binding!" ”
But there was. NET, a lot of things have changed, and one of the changes is that the bound controls that are used for data sources in rapid application development are efficient. In fact. NET makes the concept of the entire data binding go beyond the original concept of simply attaching controls to a data table. This capability is especially useful for asp.net developers, as it isolates them more from the vagaries of the application data access layer.
Data binding 101:dataset
In the simplest way, data binding involves a asp.net Web control, such as a ListBox, attached to a dataset that contains some database data. The Listbox.datasource property lets you specify the dataset to bind the control to, and the DataBind method actually populates the control with the data. Because a dataset can contain more than one field, controls with a single data column (ListBox, DropDownList, and so on) expose the DataTextField and DataKeyField properties. This allows you to specify the field name that the control will display as text or the field name used for the value.
Listing A contains a simple example that binds the listbox to the Categories table on the Northwind sample database. After the dataset was created, I bound it to ListBox1 with the DataSource property. The DataTextField property is then set to CategoryName, The latter is the field that ListBox1 should display (it will be used as Selecteditem.text), and the DataKeyField property is set to CategoryID so that ListBox1 will use it as a key. (It will be returned as Selecteditem.value.).
Data binding 201: Arrays and collections
Well, binding to a DataSet is a child's trick. But what if the data you want is not in the database? What if you want users to be able to choose from a sequence of objects? Of course, you can create a dataset that contains data manually, but it's kind of like building a tower, and what you need is a tool shed. If you just need to bind directly to an array, is this going to be good?
It is true that you can bind directly to an array of reference type variables, as listing B. In this article, I have a class--dataclass that exposes two properties, description, and IDs. I can create an array of DataClass objects and bind it to ListBox1, as I did with the dataset in listing a: Set DataTextField to Description (description), Set the DataKeyField to the ID.
Asp. NET uses the real-time reflection API to check the objects in the array and find the correct attributes, and then load them into the ListBox1 listitemcollection. Be aware that the ASP. NET is dedicated to finding members of the property--public variables or functional methods do not work here.
Listing C Description Collection can also work well, which is not really going to get you out of the way, because I'm sure by now this time, you should have been very understanding. NET is how to use IEnumerable and IEnumerator interfaces to iterate over collections. If you happen to not know what I'm saying, try to look at the listing D that binds ListBox1 to Collectionclass, which achieves iterative capabilities for similar sets by implementing IEnumerable and IEnumerator.
Data binding 301:datagrid
Data binding to the DataGrid and related controls is similar, except that you might be dealing with more than one column in the control--otherwise, why use a DataGrid? The DataGrid default binding behavior is quite intelligent: all columns (in the case of a dataset) or public properties (in the case of other objects) are displayed. So just setting the DataSource property will give you a function data binding grid.
Of course, in some cases, you don't want to use a DataGrid to create columns for yourself. You can suppress the default column-generated behavior and set the AutoGenerateColumns property to pseudo (false) so that you can control the generation and binding of columns in the DataGrid. You can then use the visual Studio. NET Column Property Editor to create columns for the grid, as shown in Figure A, Or you can do this by creating a System.Web.UI.WebControls.BoundColumn instance for each column that the DataGrid should contain.
Figure A
Visual Studio. NET provides an excellent column property editor
Listing E has rewritten the example of set bindings in Listing C to manipulate the DataGrid. I first turned off the DATAGRID1 feature in the column, and then created a new BoundColumn object. By using the DataField property, I bind the Column object to the DataClass Description property and add it to the DataGrid1 column collection.
Graduation Ceremony!
Data binding is not just an important method of rapid application development, it also helps to hide the details of your data structure into the presentation layer of your application. But using the same mechanism to bind database and object data, Microsoft makes it more useful to use data-bound controls in applications.