asp.net2.0 data binding for advanced data processing

Source: Internet
Author: User
Tags bind expression

This article describes in detail the data binding in ASP.net 2.0 advanced data processing, including binding syntax, specifying the data types that the runtime expects, how to bind to common variables and the simple properties of a page, how to bind to a property of another control, and how to bind to a common language runtime collection type , how to bind to ArrayList, how to bind to DataView, and so on.

As we discussed in the previous "data Binding in Templates" section, ASP. NET contains a declarative data-binding syntax for associating a data source field with a control's properties in a data-binding template. You can also use <%# in your code ... > syntax to perform data binding of arbitrary values, such as page and control properties, collections, expressions, and even the return result of a method call. To force the calculation of data binding values, you must call the DataBind method on the page or control that contains the data binding syntax. The following table shows some examples of data-binding syntax in asp.net.

Single Property Customer: <%# CustID%>
Set Orders
Expression Contact <%# (Customer. FirstName + "" + customer. LastName)%>
The return value of the method Outstanding Balance: <%# getbalance (CustID)%>

Although the above syntax looks similar to the ASP's Response.Write convenient syntax (<%=%>), their behavior is quite different. ASP Response.Write Convenient syntax calculates values when the page is processed, and the ASP.net data binding syntax evaluates values only when the DataBind method is invoked.

DataBind is a way to page and all control of a service. When you call the DataBind of the parent control, it calls the DataBind method of all child controls in turn. For example, Datalist1.databind () invokes the DataBind method of all controls in the DataList template. Calling the page's DataBind method--page.databind () or simply calling DataBind ()--causes all data-binding expressions on the page to be evaluated. The DataBind method is usually invoked only in the Page_Load event of the page, as shown in the following example.

In any declarative fragment of an. aspx page, you can use the binding syntax and specify the data type that the runtime expects for its valuation. The simple attributes, expressions, and methods shown in the previous example display text content to the user when they are computed. In this case, the value of the data-binding expression is of type string. In the example set above, the type of the value of the data binding syntax is the DataSource property of the listbox. You will find that the type of the cast value in the binding expression is necessary to generate the desired result. For example, if Count is an integer:

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

Asp. NET data binding syntax supports the binding of properties of public variables, page properties, and other controls on a page. The following example shows how to bind to a common variable and a simple property of a page. Note that these values have already been initialized before DataBind () is invoked.

<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
 Page.DataBind
End Sub
ReadOnly Property custID() As String
 Get
Return "ALFKI"
 End Get
End Property
ReadOnly Property orderCount() As Integer
 Get
Return 11
 End Get
End Property
</script>
<form action="DataBind1_vb.aspx" runat="server">
Customer: <b><%# custID %></b><br />
Open Orders: <b><%# orderCount %></b>
</form>

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.