Adding data to a asp.net page with data bundles

Source: Internet
Author: User
Tags contains eval expression query classic asp
Asp.net| Data | page

If you are familiar with classic ASP, you will be familiar with the data binding syntax in ASP.net, even if the functions are not the same. The data-bound expression is the code in the middle of the <%# and%> in the ASPX file. It allows data to be bound to the controller, while also allowing binding properties, expressions, and the results of method calls on the page to be bound to the controller. While this feature is easy to use, it sometimes creates confusion when determining what content is allowed to bind and whether it has been used.

Data binding elements

When the DataBind method is invoked in the asp.net of the page, the data-binding expression is linked to the properties in ASP.net, the server control properties, and the data source. When you open the server-controlled tab or anywhere on the page, you can place the expression on the value of the attribute/numeric pair. All data-binding expressions, regardless of where they are placed, are contained in the <%# and%> characters.

When using a data controller (Repeater, DataGrid, and so on), the parameter of an expression is typically the column name of the data source. In any case, whenever it returns a value, any valid expression can be used. Similarly, the same syntax can be used for external list control. This includes displaying values or assembling controller properties on the page.

Container.DataItem is a DataItem alias for a particular detail at run time. It maps a separate detail from the data source, such as getting a row from a data query or getting a single element from a vector. The actual data type of the DataItem is determined by the data source. Therefore, when dealing with an integer vector, the DataItem is also integral type.

The following list reviews some of the vb.net syntax:

<%# Container.DataItem%>--return string vector

<%# Container.DataItem ("expression")%>--returns a specific field in the DataView container

<%# Container.DataItem.PropertyName%>--Returns property values for a specific string in the data source

<%# CStr (Container.DataItem.PropertyName)%>--returns the property value and converts it to a string form

When you use C #, the syntax is slightly different. The following list contains the C # code corresponding to the list above. Note that the basic syntax is consistent, but there is a change in the return of the property value and the conversion of the data type.

<%# Container.DataItem%>

<%# ((DataRowView) Container.DataItem) ["PropertyName"]%>

<%# ((ObjectType) Container.DataItem). PropertyName%>

<%# ((ObjectType) Container.DataItem). Propertyname.tostring ()%>

The syntax is compatible when you work with the properties and methods of a page. As long as the string value is returned, the syntax is exactly the same. The following list shows an example:

<%# PropertyName%>--Returns the page Horizontal property value

<asp:listbox id= "lstvalues" Datasource= ' <%# PropertyName%> ' runat= ' server ' >--returns the page-level property values that are bound to the data controller (vector, Set of objects, etc.)

<%# (objectname.propertyname)%>--Display Page Horizontal object property values

<%# methodname ()%>--display the value returned by the page method

Use the following syntax to use a single value on the page (properties and method returned values, and so on)

<%= Value%>

The C # code in Listing a shows the data binding in the ASP.net Web table. It picks out 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 through data binding. The form headers of the table are assembled by method call. In addition, the ItemIndex properties of the DataItem are used to display the number of rows. The ItemIndex property starts at 0 and adds 1 between its display.

Listing b contains the equivalent vb.net code. The main difference is that parentheses are used in vb.net and square brackets are used in C #. and the projection of rows is not necessary in vb.net.

Using Contain.dataitem is a bit tedious, as you should be aware of the conversion of data types and data types at all times. Microsoft offers a DataBinder class that can further simplify development.

Using DataBinder

The DataBinder class is explained in detail in the Microsoft Documentation (MSDN). This approach allows the RAD designer to easily generate parsing data-binding syntax. This method can also be used for declarations of Web Forms pages to simplify mapping from one type to another.

When using data values in a ASP.net page, you can use the Eval method of the DataBinder class to get. NET to do this heavy work. The Eval method accepts the Container.DataItem object that was previously overwritten; it calculates the detailed data for each determined field in the expression and displays them. The syntax is as follows:

DataBinder.Eval (Container.DataItem, "field name", "optional formatting")

With this syntax, you can use DataBinder.Eval to override the first example, whose C # code is shown in Listing C. Listing d contains vb.net equivalent code.

DataBinder.Eval's ability to push work to the system is strong, but it is also important to note that the system locates elements and determines the time and resources of the object/data type.

Rich Options

Data binding makes it relatively easy to put data into a asp.net page. There are a number of different data-binding options to choose from: Binding data to a controller and determining how to display it, or selecting a declared data binding to a controller on a asp.net page. Finally, it's up to your liking, but there are a lot of choices that are a good thing after all.

Tony Patton, who started his career as a software developer, has passed Java, VB, Lotus, and XML certifications.



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.