Use ASP + list to bind controls (medium)

Source: Internet
Author: User

The Repeater1Page class overwrites the OnLoad method of the Page class. This indicates that on this page
DataBind is called in the first request. This causes the data binding expression on these pages to be evaluated.
The repeater control lists data sources and creates projects. Only called at the first request
DataBind method. This works properly because the repeater can
The data source instance is not required.

This page displays the public attributes of ICollection. This will be done in setting repeater
The value of DataSource is used in the data binding expression. Use package for Attribute acquisition
ArrayList that contains a sequence of SiteInfo objects. This attribute is public, because only the page class is available.
Public and protected members can be used in the data binding expression.

Each SiteInfo object has two attributes: SiteName and SiteURL. When
When the HyperLink control of is bound to data, these attributes are accessed. Binding expression of this control
Type, Container. DataItem indicates that you want to bind a specific item to a single SiteInfo on it
Object. DataBinder. Eval (Container. DataItem, "SiteName") to access the current
The SiteName attribute of the SiteInfo object.

The Repeater1 example introduces several basic concepts:

● Define a template
● Data Binding syntax and data binding expression in the template
● Use the ICollection of ArrayList as the data source
● Call the DataBind method during initial Page Processing

DataList Control

The DataList control is a templated control that uses style attributes to visualize the format.
The ability to express. It can also generate multiple column la S.

From DataList1.aspx:

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

<Asp: DataList runat = server id = "leledatalist"
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> 〉

The. aspx file displays the DataList Declaration used to generate this example.

In this example, the multi-column layout of DataList is to set the RepeatColumns attribute
Set it to "2. Setting RepeatDirection to Vertical will make the project
Top to bottom, and then arranged from left to right. Conversely, setting the value to "Horizontal" causes the project
From left to right, and then arranged from top to bottom.

The aspx syntax includes settings for the style attributes of a few DataList types. In this example,
The Width of DataList is set to 100% of its parent. Set Alternating with a gray background
ItemStyle is used to obtain the striped appearance. This example also shows that the template can contain any
Complex controls are defined to meet the needs of an ideal layout for each project.

Finally, the data binding expression in this template converts Container. DataItem to its
Type to use the pre-binding. This will not be incurred with the use of DataBinder. Eval (such as Repeater1
As shown in. However, this method may produce readability
Difference Expression. The following example also shows how to call the GetColorName method.
This page contains examples of expressions implemented in files supported by the Code.

DataList1.cs:

Namespace Samples {
...

Public class DataList1Page: Page {
Protected DataList leledatalist;

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 Control Data Source
Leledatalist. DataSource = people;

// And make the control use this data source to build its project
Leledatalist. DataBind ();
}

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

If (! IsPostBack ){
// Request this page for the first time
LoadPeopleList ();
}
}
}

Public sealed class Person {
Private string name;
Private Color favoriteColor;

Public Person (string name, Color favoriteColor ){
This. name = name;
This. favoriteColor = favoriteColor;
}

Public Color FavoriteColor {
Get {return favoriteColor ;}
}
Public string Name {
Get {return name ;}
}
}
}

On this page, the DataSource attribute of the control is set by a program
Set the relative value in declarative mode. The results of the two methods are the same. Either method is required.
You must call the DataBind method so that the control can list its data sources and create the items to represent.
.

The data source used in this example is a simple array of Person objects. Because each number
Groups all implement the ICollection method, so arrays are suitable for use as data sources. This shows
The flexibility that can be obtained when structures and types are used as data sources.

The DataList1 example introduces the following concepts:

● Define rich html ui in the template
● Use a simple array as the data source
● Set the data source through a program
● Expressions allowed in the Data Binding syntax

DataGrid Control

The DataGrid control allows you to generate a rich list representation of the data source format. In addition, it
You can also select projects with other operations.

The four examples in this section contain information about the title (title, title ID, author, price
And the publication date. All data is maintained using the xml in TitlesDB. XML. Under construction
When the table content is displayed and the book is selected, these examples follow the incremental method. Code Column
The table contains the bold text to show the changes that were made when an example was created in the previous example.

Intercepted from TitlesDB. xml:

<Root> 〉
<Schema id = "DocumentElement" targetNamespace = ""
Xmlns = http://www.w3.org/1999/XMLSchema
Xmlns: msdata = "urn: schemas-microsoft-com: xml-msdata"> "〉
<Element name = "Title"> "〉
<ComplexType content = "elementOnly"> "〉
<Element name = "title_id" type = "string"> </element> 〉
<Element name = "title" type = "string"> </element> 〉
<Element name = "au_name" type = "string"> </element> 〉
<Element name = "price" msdata: DataType = "System.
Currency"
MinOccurs = "0"
Type = "string"> </element> 〉
<Element name = "pubdate" type = "timeInstant"> "〉
</Element> 〉
</ComplexType> 〉
<Unique name = "TitleConstraint" msdata: PrimaryKey = "True"> "〉
<Selector>. </selector> 〉
<Field> title_id </field> 〉
</Unique> 〉
</Element> 〉
</Schema> 〉
<DocumentElement> 〉
<Title> 〉
<Title_id> BU1032 </title_id> 〉
<Title> The Busy Executives Database Guide </title> 〉
<Au_name> Marjorie Green </au_name> 〉
<Price> 19.99 </price> 〉
<Pubdate> 1991-06-12T07: 00: 00 </pubdate> 〉
</Title> 〉
...
</DocumentElement> 〉
</Root> 〉

In a typical Web application, to maximize scalability and performance,
It is very likely that Web services or commercial objects will be used to access data. To simplify these examples and add
Focus on using the DataGrid instead of data access. We choose to start the application
Load data at a time, and the ASP Application Status in Global. asax is slow.
As shown in the following figure.

Captured from Global. asax:

Public void Application_OnStart (){
FileStream fs = nu

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.