If you are familiar with classic ASP, then you will be familiar with the data binding syntax in ASP.NET, even if the functions are not the same. The expression of data binding is the code between <%# and %> in the ASPX file. It allows data to be bound to a controller, as well as properties, expressions, and the results of method calls on the page to be bound to the controller. Although this feature is easy to use, it sometimes causes confusion when determining what content is allowed to be bound and whether it has been used.
Data Binding Elements
When the DataBind method in ASP.NET of the page is called, the data binding expression is linked to the properties, server control properties, and data sources in ASP.NET. You can place an expression on the numeric side of an attribute value/value pair when opening a server-controlled tab or anywhere on the page. All data binding expressions, no matter where they are placed, are enclosed in <%# and %> characters.
When using a data controller (Repeater, DataGrid, etc.), the parameter of the expression is usually the column name of the data source. Regardless, any valid expression can be used as long as it returns a value. Likewise, the same syntax can be used for external list controls. This includes displaying values on the page or assembling controller properties.
Container.DataItem is a runtime alias for DataItem for a specific item. It maps a single item from a data source, such as a row from a data query or a single element from a vector. The actual data type of the DataItem is determined by the data source. Therefore, when dealing with a vector of integers, the DataItem is also an integer.
The following list reviews some VB.NET syntax situations:
<%# Container.DataItem %>--returns a string vector
<%# Container.DataItem("expression") %>--returns a specific item in the DataView container Field
<%# Container.DataItem.PropertyName %>--returns the property value of a specific string in the data source
<%# CStr(Container.DataItem.PropertyName) %>--returns the property value and converts it into a string form
when using C# , the syntax is slightly different. The following list contains the C# code corresponding to the above list. Note that the basic syntax is the same, but there are changes in the conversion of returned attribute values and data types.
<%# Container.DataItem %>
<%# ((DataRowView)Container.DataItem)["PropertyName"] %>
<%# ((ObjectType)Container.DataItem).PropertyName %>
<%# ((ObjectType)Container. DataItem).PropertyName.ToString() %>
The syntax is consistent when acting on page properties and methods. As long as a string value is returned, the syntax is exactly the same. The following list gives an example:
<%# propertyName %>--Returns page-level property values
<asp:ListBox id="lstValues" datasource='<%# propertyName %>' runat="server">--Returns the same as Page level property values (vectors, object sets, etc.) bound to the data controller
<%# (objectName.PropertyName) %>--Display page level object property values
<%# MethodName() %>--Display page method returns Values
Use the following syntax to use a single value on the page (values returned by properties and methods, etc.)
<%= Value %>
The C# code in Listing A illustrates data binding in an ASP.NET Web form. It selects the employee's name and phone number from the SQL Sever Northwind Employees table. The query value is displayed through the ASP.NET Repeater controller. Column values are inserted via data binding. The form title of the table is assembled through a method call. In addition, the ItemIndex property of Dataitem is used to display the number of rows. The ItemIndex property starts at 0 and is incremented by 1 between displays.
Listing B contains the equivalent VB.NET code. The main difference is that VB.NET uses parentheses and C# uses square brackets. And row casting is unnecessary in VB.NET.
Using Contain.DataItem is a bit tedious, because you have to pay attention to data types and data type conversion at any time. Microsoft provides the DataBinder class, which can further simplify development.
Using the DataBinder
DataBinder class is explained in detail in the Microsoft documentation (MSDN). This method allows the RAD designer to easily generate parsed data binding syntax. This method can also be used in Web Forms page declarations to simplify mapping from one type to another.
When using data values in an ASP.NET page, you can use the Eval method of the DataBinder class to let .NET do the heavy lifting. The Eval method accepts the previously overridden Container.DataItem object; it calculates the detailed data for each specified field in the expression and displays them. The syntax is as follows:
DataBinder.Eval(Container.DataItem, "field name", "optional formatting")
Using this syntax, you can rewrite the first example using DataBinder.Eval. See Listing C for its C# code. Listing D contains VB.NET equivalent code.
DataBinder.Eval is very powerful in pushing work to the system, but also be aware that the time and resource overhead of the system locating elements and determining object/data types is high.
Rich option
data binding makes putting data into ASP.NET pages relatively simple. There are many different data binding options to choose from, including: binding data to a controller and determining how to display it, or choosing to bind data to a controller declared on the ASP.NET page. In the end, it comes down to your preference, but having a lot of options is ultimately a good thing.
Tony Patton began his career as a software developer and has been certified in Java, VB, Lotus, and XML.