Use ASP + list to bind controls (on)

Source: Internet
Author: User

Introduction

The Repeater, DataList, and DataGrid controls form the System. Web.
The Web control set in the UI. WebControls namespace. These controls bind HTML display
List or data source content. Therefore, they are collectively referred to as "list binding controls ".

And other Web

Similar to controls, these controls provide consistent programming models and encapsulate a browser-independent
Indicates the logic. These features enable developers to program object models without having to master
The inconsistent and complex technical knowledge related to HTML.

These three controls can be laid out in multiple la S (including list, column/newspaper column, and stream layout)
(HTML stream) to indicate the content of the data source. In addition, they allow you to complete
Completely different or completely customized la S. In addition to encapsulating the representation logic, it also provides
Sent data, execution status management, and event triggering. Finally, they also,
Standard operations such as editing, paging, and sorting provide support at different levels. These controls can be simplified
Provides several common Web application solutions, including reports, shopping carts, product lists, and queries.
Result and navigation menu.

The following sections further describe these controls and how to use them in your Web application.
And how to select controls.

How does list binding control work?

This section is the background material for the rest of this article. This topic describes how to bind controls to these lists.
And their common features and some related concepts.

DataSource Properties
Each control has a DataSource property of the System. Collections type.
ICollection. In the simplest case, the data source is a list or set of similar objects.

Several objects in this framework provide ICollection implementation. This set includes
System. Data. DataView (which is usually used to access relational databases and XML Data ),
ICollection implementation (such as ArrayList and Hashtable) and array.

Unlike traditional data binding controls (which usually require an ADO record set ),
In addition to the ICollection interface, these list binding controls do not impose any additional requirements on their data sources
. According to the design, by adding a large number of types and data that can be used as valid DataSource attribute values
Structure, which maximizes the simplicity and flexibility of your application code.

Project set

Each list-bound control contains a project set. Control by listing the current objects
In the project set. Create a single project for each object in the list and use it for the table
Indicates the object. These items are also part of the control hierarchy contained in the List-bound control.
Points.

The following table lists the project types associated with the data source.

Default project type created by the Project
AlternatingItem is created for a project with an odd number of underlying items in the project set.
SelectedItem is created for the selected project (whether or not the project is an alternative project)
EditItem is created for a project in editing mode (whether selected or not)


The control also creates the following items that will be used in the representation. However, they are not associated with the data source
.

Header used to indicate header information
The footer indicates the footer information.
The Delimiter is used to represent the content between each project as shown in figure 1, and only applies to Repeater and DataList
The pagination operator is used to indicate the page UI associated with the DataGrid Control.

    

Project Data Binding and Creation

The list binding control follows the explicit data binding model implemented in the entire ASP + framework. This
This means that the control only needs to list its data sources when its DataBind method is called.

When DataBind is called

The list control lists its data sources, creates projects, and extracts
Value to initialize it. If status management is enabled, the control also saves the required
To re-create the project during the page's return processing without re-configuring the data
Source.

The explicit data binding model allows your application code to be precisely identified in the processing sequence
When and where data sources are required. This function enables less and more efficient access to database servers,
These accesses are usually the most resource-consuming operations of Web applications.

The general rule is that DataBind must be called every time you create a new project. In most
In this case, you will call DataBind when your page is requested to create an initial project set for the first time.
In the subsequent execution of this page, you will need
This method is called in the handler. When the query used to create the initial data source is modified
This happens. When the project status changes (for example, from read-only mode to edit mode ),
This can happen.

Style

By using style attributes on the object model, you can define all DataList and DataGrid
Controls and the format and appearance of the items they contain. These attributes allow custom fonts, colors, and borders
And other Appearance factors. Control style attributes (such as foreground color, background color, Font, and border)
Style) will affect the representation of the entire control.

In addition, each control contains a large number of style attributes that match the project type it created,
For example, as ItemStyle, AlternatingItemStyle, and headerStyle. DataGrid provides
Level 3 style attributes that affect all cells in a specific column. The
Each column can have its own headerStyle, FooterStyle, and ItemStyle.

Template

The style control format is displayed, while the template defines the content and representation of each project. You can
The template, as an HTML code snippet, defines the control hierarchy used to represent the project.

The Repeater and DataList controls are driven by the template you specified and provide various settings
Set the template attributes, such as ItemTemplate, AlternatingItemTemplate, and header.
Template. Similar to a style, each template corresponds to a specific type of project.

The DataGrid Control is not templated. However, the Template in the Column set of the control
Columns makes it possible to use the template in the DataGrid. Each ticket in TemplateColumn
The cell can contain a template, which is similar to the project pole in the Repeater or DataList control.
Is similar. This also makes the custom representation in the DataGrid possible.

Data Binding in the template

The template defines the control hierarchy contained in the project. By using a data binding expression
Control Properties in the hierarchy can be bound to the data properties associated with this project.

A project that is the logical parent of a template is called a "Container" in a data binding expression ". Each
Each container has a property called DataItem, which references its associated data. Result
Yes. Most typical data binding expressions in the template bind the control property to the Container.
On an attribute of DataItem. The binding is further described in the following sections.

Repeater control

As described above, the Repeater control is fully template-driven and allows creation of fully customizable
Format and layout. This function is described.

From Repeater1.aspx:

<% @ Page language = "C #" src = "Repeater1.cs" inherits = "Samples.
Repeater1Page "%> 〉
...

<Asp: Repeater runat = server id = "linksListRepeater"
DataSource = <%# SiteLinks %> 〉〉
<Template name = "headerTemplate"> "〉
<Ul type = "1"> "〉
</Template> 〉
<Template name = "ItemTemplate"> "〉
<Li> 〉
<Asp: HyperLink runat = server
Text = <%# DataBinder. Eval (Container. DataItem, "SiteName ")
%> 〉
NavigateUrl = <%# DataBinder. Eval (Container. DataItem,
"SiteURL") %> 〉〉
</Asp: HyperLink> 〉
</Li> 〉
</Template> 〉
<Template name = "FooterTemplate"> "〉
</Ul> 〉
</Template> 〉
</Asp: Repeater> 〉

This. aspx file displays a repeater control used to generate a list of bullets
Statement.

This example shows how to set the data source declaration using the Data Binding syntax (<%#... %>.
When you call the DataBind method, the expressions in the data binding will be executed. In this situation
The DataSource attribute of repeater is bound to the SiteLinks attribute of the page,
The latter contains the URL reference to be displayed.

Repeater is the only control that allows HTML fragments to exist in its template.
The combination of controls and HTML fragments produces a good form of HTML. In this example
The number list is divided into three parts:

Marked by the list indicated by headerTemplate (<ul type = "1"> "〉).

End tag of the list, represented by FooterTemplate (</ul> 〉).

The body of the List repeats the Item
The list items (<li>) generated by the Template are placed.

You can also use these templates to specify the TABLE start tag (<TABLE>) in the header.
Specify the end mark of the TABLE (</TABLE>) in the script, and specify a single TABLE row (<TR>) in each project 〉). This replacement option will result in a list representation.

You must specify ItemTemplate. It is the only required template. When no other modulo is specified
The widget automatically uses this ItemTemplate for other templates.

In the following example, ItemTemplate contains a HyperLink Web control. This control
The Text and NavigateUrl attributes of the file are bound to the data attributes associated with each duplicate project.
. This is when the data binding expression is used (evaluate the expression immediately after the project is created ).
.

Repeater1.cs:

Namespace Samples {
...

Public class Repeater1Page: Page {
Protected Repeater linksListRepeater;

Public ICollection SiteLinks {
Get {
ArrayList sites = new ArrayList ();

Sites. Add (new SiteInfo ("Microsoft Home ",
"Http://www.microsoft.com "));
Sites. Add (new SiteInfo ("MSDN Home ",
"Http://msdn.microsoft.com "));
 

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.