To bind a control using the asp+ list (middle)

Source: Internet
Author: User
Tags bind eval expression
The asp+| control Repeater1page class overrides the OnLoad method of the Page class. This representation on the page
Call DataBind in the first request. This will result in a data-binding expression on these pages seeking
Value and causes the Repeater control to enumerate the data source and create its project. Called only on first request
DataBind method. This works because repeater is able to protect
Recreate its project in the return process of a stored state without requiring a data source instance.

This page exposes the common properties of type ICollection. This will be in the settings repeater
Used in data-binding expressions for DataSource property values. Property gets implemented using the package
A ArrayList containing a set of Siteinfo object sequences. This property is public because only the page class
's public and protected members can be used in data-binding expressions.

Each Siteinfo object has two properties: SiteName and SiteURL. When you are in the template
Hyperlink controls will access these properties when they are data bound. The binding expression of this control
Container.DataItem represents a single siteinfo on which a particular item is to be bound
Object. DataBinder.Eval (Container.DataItem, "SiteName") accesses the current
The SiteName property of the Siteinfo object.

The Repeater1 sample introduces you to a few basic concepts:

Defining templates
Data-binding syntax and data-binding expressions in templates
To use the ICollection representation of ArrayList as a data source
Calling the DataBind method during the initial processing of a page

DataList Control

The DataList control is a templated control that provides visual formatting using style attributes
The ability to express it. It can also produce multiple-column layouts.

Excerpt from datalist1.aspx:

〈%@ Page language= "C #" src= "DataList1.cs" inherits= "Samples.
Datalist1page "%〉
...

〈asp:datalist runat=server id= "Peopledatalist"
repeatcolumns= "2" repeatdirection= "Vertical" repeatmode= "Table"
Width= "100%"

〈property name= "AlternatingItemStyle"
〈asp:tableitemstyle backcolor= "#EEEEEE"/〉
〈/property〉
〈template name= "ItemTemplate"
〈asp:panel runat=server font-size= "12pt" font-bold= "true"
〈%# (person) container.dataitem). Name%〉
〈/asp:panel〉
〈asp:label runat=server width= "20px"
Borderstyle= "Solid" borderwidth= "1px" bordercolor= "BLACK"
Backcolor= ' 〈%# (person) container.dataitem). FavoriteColor
%〉'〉
〈/asp:label〉

〈asp:label runat=server font-size= "10pt"
Text= ' 〈%# getcolorname ((person) container.dataitem).
FavoriteColor)%〉 '
〈/asp:label〉
〈/template〉
〈/asp:datalist〉

this. aspx file shows the declaration of the DataList used to generate this example.

In this example, DataList's multiple-column layout is done by setting the RepeatColumns property
Set to "2" to achieve. Setting RepeatDirection to "Vertical" causes the project to be
Up and down, then from left to right. Conversely, setting the value to "horizontal" causes the project
Arrange from left to right, then top to bottom.

The ASPX syntax contains settings for style attributes for a few DataList. In this example,
The width of the DataList is set to 100% of its parent. Set up a alternating with a gray background
The ItemStyle is designed to get a striped look. This example also shows that a template can contain any
Complex control definitions to meet the need for an ideal layout within each project.

Finally, the data-binding expression in this template converts container.dataitem to its
Type to use early binding. This will not incur the use of databinder.eval (such as Repeater1
The cost of the associated late binding as shown in. However, this method may produce more readable
The expression of the difference. The following example also gives a call to the Getcolorname method, which is
An example of an expression that is implemented in a file supported by code on this page.

DataList1.cs:

Namespace Samples {
...

public class Datalist1page:page {
protected DataList peopledatalist;

Protected string Getcolorname (Color c) {
Return
Typedescriptor.getconverter (typeof (Color)). Convert
ToString (c);
}

private void Loadpeoplelist () {
Create a data source
Person[] people = new person[] {
New Person ("Nikhil Kothari", Color.green),
New Person ("Steve millet", color.purple),
New Person ("Chris Anderson", Color.Blue),
New Person ("Mike Pope", Color.orange),
New Person ("Anthony Moore", Color.yellow),
New Person ("Jon Jung", Color.mediumaquamarine),
New Person ("Susan Warren", Color.slateblue),
New Person ("Izzy Gryko", color.red)
};

Set the data source for a control
Peopledatalist.datasource = people;

And make the control use this data source to build its project
Peopledatalist.databind ();
}

protected override void OnLoad (EventArgs e) {
Base. OnLoad (e);

if (! IsPostBack) {
First request this page
Loadpeoplelist ();



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.