Asp. NET hands-on Tutorials (8)

Source: Internet
Author: User
Tags format bind count eval expression integer variables

ASP.net introduces a new syntax for declaring data binding. This extremely flexible syntax allows developers to bind not only the data source, but also simple properties, collections, expressions, and even the results returned by the calling method. The following list shows some examples of this new syntax:
Simple properties

Customer: <%# CustID%>

Collection

Orders: <asp:listbox id= "List1" Datasource= ' <%#

MyArray%> ' runat= ' server >

An expression

Contact: <%# (Customer. The Name + "" + customer. LastName

)%>

Method results

Outstanding Balance: <%# getbalance (CustID)%>

Although this syntax appears to be similar to the <%=%> of Response.Write in ASP, their operating mechanism is quite different. Response.Write's shorthand syntax is evaluated when the page is processed, and the ASP.net data binding syntax is evaluated only when the DataBind method is invoked.

DataBind is a method of page and all server controls. When you call the DataBind method on a parent control, the child controls that he contains call the DataBind method. For example, to perform datalist1.databind (), all controls in the DataList template refer to the DataBind method. When the page calls the DataBind method---page.databind () or a simple DataBind ()---causes all data-binding expressions on the page to be evaluated. DataBind are often invoked in the Page_Load event, such as the following example:

protected void Page_Load (Object Src, EventArgs E) {
DataBind ();
}

You can use a data-binding expression almost anywhere in the declaration section of an. aspx page to provide the expected data type it evaluates at run time. When evaluated, simple properties, expressions, and the methods in the previous example show the text to the user. In these cases, the data-binding expression must find a string-type value. In the example of a collection, the type of data-binding expression evaluated is valid for the DataSource property of the listbox. You will find it necessary to cast the type of the result value in a data-binding expression in order to get the desired result. For example, if Count is an integer:

Number of Records: <%# count. ToString ()%>

Binding Simple Properties

asp.net data-binding syntax supports common variables and properties of bound pages, as well as properties of other controls on the page.

The following examples illustrate common variables and simple properties that are bound to a page. Note that these values were initialized before DataBind () was invoked.

Binding collections and Lists

List server controls, such as DataGrid, ListBox, and HtmlSelect, use collections as data sources. The following example shows the binding to a commonly used CLR collection type. These controls can only be bound to a collection that supports IEnumerable, ICollection, or IListSource interfaces. Typically, you can bind to ArrayList, Hashtable, DataView, and DataReader.

Most of the time, you might be able to manipulate the data before you bind to the page or control.

DataBinder.Eval method

The ASP.net framework provides a static method DataBinder.Eval that calculates the value of a late data-binding expression and can format the result arbitrarily as a string. DataBinder.Eval is convenient, and he excludes explicit conversions that many developers must make (by forcing the type of value to get the expected data type). Especially in data-bound controls with a template list, this is especially useful because you often need to explicitly convert data rows and data fields.

Take a closer look at the code below and the integer will be displayed as a currency type string. Using the standard asp.net data binding syntax, in order to get the data field IntegerValue, you must first explicitly convert the type of the data row, and then as a parameter of the String.Format method to get the result

<%# String.Format ("{0:c}", ((DataRowView) container.dataitem) ["IntegerValue"])%>

This kind of grammar is really complicated and difficult to remember. Comparatively speaking, the DataBinder.Eval is very simple. It comes with three parameters: the naming container for the data item, the data field name, and the formatted string. Naming containers are always container.dataitem in a list of templates such as DataList, DataGrid, or Repeater. The Page is another naming container that can be used by DataBinder.Eval.

<%# DataBinder.Eval (Container.DataItem, "IntegerValue", "{0:c}")%>

The format string parameter is optional. If you omit the argument, DataBinder.Eval returns the value of the object type, as in the following code:

<%# (BOOL) DataBinder.Eval (Container.DataItem, "Boolvalue")%>

It is important to note that due to late binding effects, DataBinder.Eval has a significant difference in execution efficiency compared to the standard data-binding syntax. So selectively use DataBinder.Eval, especially if you don't need to format strings.



Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.