Template control in data access

zhaozj2021-02-16  93

Although the DataGrid server control is very suitable for many web applications that are suitable for mesh styles data representations, but in many cases, data representation needs to be rich. ASP.NET provides two controls DataList and Repeater, which provides greater flexibility for the presentation of the list style. These controls are based on templates, so there is no form of default presentation. The presentation method of data is completely dependent on the implementation of the control template, and the control template describes how to represent data items.

Like the DataGrid control, DataList and Repeater also support DataSource properties, which can be set to any iCollection, IEnumeRable, or IListSource type. The data in this DataSource is bound to the control through its DATABIND method. After binding the data, the format of each data item is described by the template.

The ItemTemplate property controls the presentation form of each item in the DataSource collection. Any arbitrary representation code (HTML or other) can be defined within ItemTemplate. Using the ASP.NET data binding syntax, you can insert the value in the data binding into the DataList or Repeater control, as shown in the following example.

hello <% # databinder.eval (container.DataItem, "name")%>!

Container represents the first control that supports the System.Web.UI.inamingContainer tag interface in the direct hierarchy. In this example, Container is parsed to the object of System.Web.ui.WebControls.RepeaterItem type, and the type has a DataItem property. When Repeater iterates on the DataSource collection, the DataItem consists of the current item of the collection. For example, if the data source is set to the ArrayList of the Employee object, DataItem is the Employees type. DataItem is the DataRowView type when binding to DataView.

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

New Post(0)