DataBinder.eval and data binding technology in .NET

xiaoxiao2021-03-06  124

What is the purpose of finding "<% #".

ASP.NET declarative data binding syntax uses <% #%> representation.

The following content comes from:

Http://mirwang.yculblog.com/post-188640.html

However, it seems to be quoted by others, and the example connection is invalid.

Data Binding Server Control

Data Binding Overview and Syntax

Bind to simple properties

Bind to collections and lists

Binding expressions or methods

DataBinder.eval ()

This section is summarized

-------------------------------------------------- ------------------------------

Data Binding Overview and Syntax

ASP.NET introduces new declarative data binding syntax. This very flexible syntax allows developers to not only bind to data sources, but also bind to simple properties, collections, expressions or even results returned from methods. The following table shows some examples of new grammar.

Simple Attributes Customer: <% # CustId%>

Collection Orders:

Expression Contact: <% # (Customer.First Name " Customer.lastname)%>

Method: OutStanding Balance: <% # getBalance (Custid)%>

Although this syntax looks like the ASP's response.write shortcut <% =%>, its behavior is completely different. ASP Response.write Shortcut Syntax Calculates when the page is processed, and the ASP.NET data binding syntax is calculated only when the DataBind method is called.

Database is a way pages and all server controls. When you call DataBind on the parent control, it cascades all child controls for the control. For example, DataList1.DATABIND () will then call the DataBind method for each control in the DataList template. Call data on page on the page () or just databind () - will cause all data binding expressions on the calculation page. Database from the Page_Load event is usually called, as shown in the following example.

Protected Void Page_Load (Object SRC, Eventargs E) {

DataBind ();

}

Protected Sub Page_Load (SRC As Object, E AS Eventargs)

DataBind ()

End Sub

Protected Function Page_Load (src: Object, E: Eventargs): void {

DataBind ();

}

C # VB JScript

If the binding expression is calculated as the expected data type at runtime, you can use the binding expression in almost anywhere in the .aspx page. The above simple properties, expressions, and method examples displays text to the user when calculating. In these cases, the data binding expression must be calculated as the value of the String type. In a collection example, the data binding expression calculates the valid type value of the DataSource attribute of ListBox. You may find that it is necessary to convert the type value in the binding expression to generate the desired result. For example, if count is an integer: Number of Records: <% # count.toString ()%>

Bind to simple properties

ASP.NET Data Binding Syntax Supports Bind to public variables, attributes of page pages, and other controls on page.

The following example shows how to bind to the simple properties of the public variables and pages. Note that these values ​​initialize before the data call () call.

VB DataBind1.aspx

[Run example] | [View Source Code]

The following example illustrates how to bind the properties of another control.

VB DataBind2.aspx

[Run example] | [View Source Code]

Bind to collections and lists

A list of server controls like DataGrid, ListBox, and HTMLSELECT use as a data source. The following example shows how to bind to the usual public language run library collection type. These controls can only be bound to the collection of IENUMERABLE, ICOLLECTION, or ILISTSOURCE interfaces. The most common is to bind to ArrayList, Hashtable, DataView, and DataReader.

The following example shows how to bind to ArrayList.

VB DataBind3.aspx

[Run example] | [View Source Code]

The following example shows how to bind to DataView. Note that the DataView class is defined in the System.Data namespace.

VB DataBind4.aspx

[Run example] | [View Source Code]

The following example illustrates how to bind to HashTable.

VB DataBind5.aspx

[Run example] | [View Source Code]

Binding expressions or methods

You usually need to operate data before binding to page or controls. The following example illustrates how to bind to the return value of the expression and method.

VB DataBind6.aspx

[Run example] | [View Source Code]

DataBinder.eval

The ASP.NET framework provides a static method that calculates the post-bound data binding expression and can be selected to format the results as a string. DataBinder.eval is very convenient, because it eliminates many explicit conversions that developers must do for forced to convert values ​​to desired data types. This is especially useful when the controls within the data binding template list, as the usual data lines and data fields must be converted.

Please see the example below, the integer in this example will appear as a currency string. With standard ASP.NET data binding syntax, you must first convert the type of data line to retrieve the data field Integerue. Next, this as a parameter is passed to the String.Format method.

<% # String.format ("{0: C}", (DATAROWVIEW) Container.DataItem) ["Integerue"])%>

<% # String.format ("{0: C}", (CType (Container.DataItem, DataRowView)))%>

<% # String.format ("{0: C}", ("IntegerValue") ["Integerue"]%>

C # VB JScript

This syntax may be more complicated and difficult to memorize. Instead, DataBinder.eval is just a method with three parameters: Named container, data field name, and format string of data items. In a template list like DataList, DataGrid, or Repeater, the named container is always Container.DataItem. Page is another named container that can be used with DataBinder.eval. <% # DataBinder.eval (Container.DataItem, "Integerue", "{0: C}")%>

<% # DataBinder.eval (Container.DataItem, "Integerue", "{0: C}")%>

<% # DataBinder.eval (Container.DataItem, "Integerue", "{0: C}")%>

C # VB JScript

The format string parameter is optional. If it is omitted, DataBinder.eval returns the value of the object type, as shown in the following example.

<% # (BOOL) DataBinder.eval (Container.DataItem, "BoolValue")%>

<% # Ctype (dataBinder.eval (Container.DataItem, "BoolValue"), Boolean%>

<% # Boolean (dataBinder.eval (Container.DataItem, "BoolValue"))%>

C # VB JScript

DataBinder.eval will bring significant performance losses to standard data binding syntax, as it uses later binding reflections, pay attention to this is important. Be cautious when using DataBinder.eval, especially when a string format is not required.

VB DataBind7.aspx

[Run example] | [View Source Code]

This section is summarized

ASP.NET declarative data binding syntax uses <% #%> representation.

You can bind to data sources, page, or other controls, collections, expressions, and results returned from way to the method.

List controls can be bound to a collection that supports iCollection, IENUMERABLE, or ILISTSource interface, such as ArrayList, Hashtable, DataView, and DataReader.

DataBinder.eval is a static method for advanced binding. Its syntax may be simpler than the standard data binding syntax, but the performance is low.

转载请注明原文地址:https://www.9cbs.com/read-97575.html

New Post(0)