Server | control | Data data Binding server control
Data Binding Overview and syntax
ASP.net introduces a new declarative data-binding syntax. This very flexible syntax allows developers not only to bind to a data source, but also to bind to a simple property, a collection, an expression, or even a result returned from a method call. The following table shows some examples of the new syntax.
Although the syntax looks similar to the ASP's Response.Write shortcut <%=%>, its behavior is completely different. ASP Response.Write Shortcut syntax is evaluated while the page is being processed, and asp.net data-binding syntax is evaluated only when the DataBind method is invoked.
DataBind is the method for pages and all server controls. When DataBind is invoked on a parent control, it is cascaded to all child controls of the control. For example, Datalist1.databind () will therefore invoke the DataBind method for each control in the DataList template. Calling Databind-page.databind () or just DataBind () on a page causes all data-binding expressions on the calculated page to be evaluated. DataBind is usually called from the Page_Load event, as shown in the following example.
Protected Sub Page_Load (Src as Object, E as EventArgs)
DataBind ()
End Sub
protected function Page_Load (Src:object, E:eventargs): void {
DataBind ();
}
If the binding expression evaluates to the expected data type at run time, you can use the binding expression in almost any location in the declaration section of the. aspx page. The simple properties, expressions, and method examples above display text to the user when evaluated. In these cases, the data-binding expression must evaluate to a String-type value. In the collection example, the data-binding expression evaluates to a valid type value for the DataSource property of the ListBox. You may find it necessary to convert the value of the type in the binding expression to produce the desired result. For example, if Count is an integer:
Number of Records: <%# count. ToString ()%>
Binding to Simple properties
asp.net data-binding syntax supports properties that are bound to public variables, page properties, and other controls on the page.
The following example shows how to bind to a common variable and a simple property on a page. Note that these values are initialized before the DataBind () call.
Call only "Page.DataBind", not from "StateList"
To explicitly remove the variable, and then manipulate the label control.
This calculates all the <%#%> expressions in the page
List server controls such as the DataGrid, ListBox, and HtmlSelect use the collection as a data source. The following example shows how to bind to the usual common language runtime collection type. These controls can only be bound to a collection that supports IEnumerable, ICollection, or IListSource interfaces. The most common is binding to ArrayList, Hashtable, DataView, and DataReader.
The following example shows how to bind to a ArrayList.
You typically need to manipulate data before you bind to a page or control. The following example shows how to bind to the return value of an expression and a method.
The ASP.net framework provides a static method for calculating late-bound data-binding expressions and optionally formatting the result as a string. DataBinder.Eval is convenient because it eliminates many of the explicit conversions that developers must do to force the value to be converted to the desired data type. This is especially useful in controls that are in the list of data-bound templates, because the types of data rows and data fields typically must be converted.
Take a look at the example below, in this case the integer will appear as a currency string. Using the standard asp.net data binding syntax, you must first convert the type of the data row to retrieve the data field IntegerValue. Next, pass this as a parameter to the String.Format method.
This syntax may be more complex and difficult to remember. Instead, DataBinder.Eval is just a method with three parameters: the name container for the data item, the data field name, and the format string. In a list of templates such as DataList, DataGrid, or Repeater, the naming container is always container.dataitem. The Page is another naming container that you can use with DataBinder.Eval.
DataBinder.Eval has a significant performance penalty for standard data-binding syntax because it uses late-bound reflection, which is important to note. You need to be cautious when using databinder.eval, especially if you don't need string formatting.
<%@ Import namespace= "System.Data"%>
<script language= "C #" runat= "Server" >
void Page_Load (Object sender, EventArgs e) {
if (! Page.IsPostBack) {
DataTable dt = new DataTable ();
DataRow Dr;
Dt. Columns.Add (New DataColumn ("IntegerValue", typeof (Int32));
Dt. Columns.Add (New DataColumn ("StringValue", typeof (String));
Dt. Columns.Add (New DataColumn ("Datetimevalue", typeof (DateTime));
Dt. Columns.Add (New DataColumn ("Boolvalue", typeof (BOOL));
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.