Data Binding Server Control
Data Binding Overview and Syntax
ASP.NET introduces a new grammar for declarative data binding. This extremely flexible syntax allows developers not only binding numbers
According to the source, but also the simple attributes, collections, expressions, and even the result returned by the method. Below
The list shows some examples of this new grammar: Simple Properties Customer:
Collection ORDERS:
Expression Contact:
Method: Outstanding Balance:
Although this syntax looks similar to the shorthand of Response.Write in the ASP, their running machine
It has greatly different. Response.write's shorthand syntax evaluation is based on page processing, while ASP.NET data binding
The law only value when the DataBind method is called.
Database is a method for Page and all server controls. When you call a DATABIND method for a parent control
Waiting, the child controls he contain will call the DataBind method. For example, perform DataList1.Database (), Datali
All controls in the ST template reference DataBind methods. Call DATABIND Method in Page --- Page.Databin
D () or simple databind () --- results in all data binding expressions on the page will be evaluated. DataBind
Often called in the page_load event, such as the example below:
Protected Void Page_Load (Object SRC, Eventargs E) {
DataBind ();
}
You can use the data binding expression anywhere in the .aspx page, provide it at runtime
Expected data type. When evaluating, simple attributes, expressions, and methods in the above example will be displayed
Demon text. In these cases, the data binding expression must obtain a value of a String type. In the example of the collection
The type of data binding expression is valid for ListBox's DataSource properties. You will send
Now, in order to obtain the desired result in the data binding expression, it is necessary to force the type of conversion result value.
For example, if count is an integer:
Number of Records:
Binding simple properties
ASP.NET data binding syntax supports the public variables and properties of the binding page, and the properties of other controls on the page.
The following example illustrates the common variables and simple properties that are bound to the page. Note that these values are called DATABIND ().
Has been initialized.
C # databind1.aspx
[Run] | [Source File]
The following example describes the properties that bind to other controls.
C # databind2.aspx
[Run] | [Source File]
Binding collection and list
List server controls such as DataGrid, ListBox, and HTMLSELECT, using collections as a data source. Below
Example shows that binding to a commonly used CLR collection type. These controls can only be bound to support IENUMERABLE, ICOLLECTI
ON, or collection of iListSource interface. Often, you can bind to ArrayList, Hashtable, DataV
IEW and DataReader.
The next example is bound to arraylist.
C # databind3.aspx
[Run] | [Source File]
The following example illustrates the bound to the dataview. Note that the DataView class is defined in the System.Data Name Space
C # databind4.aspx
[Run] | [Source File]
The following example illustrates the bind to havehtable.
C # databind5.aspx
[Run] | [Source File]
Binding expressions or methods
Many times, you may operate these data before binding to page or control. The following example shows that it is bound to one
A expression and return the value of the method.
C # databind6.aspx
[Run] | [Source File] DataBinder.eval Method
The ASP.NET framework provides a static method DataBinder.eval, which can calculate the value of the post-data binding expression.
And can be arbitrarily formatted as a string. DataBinder.eval is very convenient, he excludes many developers
It must be made (by enforcing the type of data to get the expected data type) explicit conversion. Especially in template
Among the list of data binding controls, it is especially useful because it often needs to explicitly convert the data lines and data fields.
Look carefully the following code, the integer will be displayed as a currency string. Use the standard ASP.NET data binding syntax,
Get the data field integerue, you must first explicitly convert the type of data, then as string.for
The parameter of the MAT method can get the result
Such syntax is more complex and difficult to memorize. Comparison, DataBinder.eval is very simple. It has
Three parameters: Name container, data field name, and format string of data items. In the template list such as DataList,
DataGrid, or Repeater, named container is always container.DataItem. Page is another can be dat
ABINDER.EVAL uses a named container.
Formatted strings are optional. If you ignore the parameters, DataBinder.eval returns the value of the object type, just like
The following code is:
It is important to note: Due to the post-binding effect, DataBinder.eval is bound to standard data binding grammar
Comparison, there is a significant difference in performing efficiency. So selectively use DataBinder.eval, especially without the words
When the string is formatted.
C # databind7.aspx
[Run] | [Source File]
chapter summary
l The ASP.NET uses symbol declaration data binding syntax.
l You can bind data to data sources, pages, or other controls, collections, expressions, and from ways to
result.
l The list control can be bound to a collection that supports Icollection, IEnumeRable, or ILISTSource interface, for example
ArrayList, Hashtable, DataView and DataReader.
l DataBinder.eval is a post-bound static method. His grammar is easier than standard data binding, but
It is a difference in performance.