ASP.NET is immediately going to tutorial 12

xiaoxiao2021-03-05  20

The computer upgrades have been upgraded in recent days and reinstall all things including the operating system. Plus a problem in the middle

So this part of the translation stopped for several days, today.

Data Access and Customization

Template control introduction

The DataGrid control of the previous sections, for many web applications that need to be displayed using similar forms

The program is very suitable. But many times, you need to use a richer form to display data. ASP.NET provides two

Control, DataList, and Repeater provide very flexible control of data for similar lists. These two

The control is template based, so there is no default performance. The method of implementing data is completely executed by the control template,

This template describes how to present data items.

Similar to the DataGrid control, DataList, and Reater supports the DataSource property, this property can be set to

Any iCollection, IENUMERABLE, or ILISTSOURCE type. Data in DataSource (Data Source)

You can use the DATABIND method to bind. Once the data is bound, the format of each data item can be described by the template.

.

The ItemTemplate property controls the presentation of each item in the data source collection. Inside ItemTemplate, you can define

Any expression code (HTML or other).

Use the ASP.NET data binding syntax, you can insert the value to the DataList or Repeater control from the data binding

Just like this.

Hello!

Container inherits from the namespace system.web.ui.inamingcontainer. In this case, Con

TAINER parsing is an object of a system.web.ui.webcontrols.repeaterItem type, he has a DAT

AITEM properties. Just like the REPEATER repetition data source collection, DataItem contains the current item in the collection. example

For example, if the data source of the employee field object is set to arraylist, DataItem is the type of employee. Bind

To DataView, DataITEM is a DataRowView type.

The following example demonstrates an REATER control to bind to DataView (returning from SQL query). At the same time, two

Template: HeadeRTemplate and Footertemplate, used to show the beginning and end of the list.

C # DataList1.aspx

[Run] | [Source Code]

The Repeater control is used to repeat the data in the data source collection, using a Item for each item in the data source collection.

Template, but does not show any elements that are not included in the template. Dress data relative to Repeater, DAT

Alist provides some additional features to control the layout of the list. Unlike Repeater, DataList uses additional

Grammatical elements, such as rows, columns, columns, columns, columns, columns, etc., and enrich the format of the template definition

. For example, DataList supports the RepeatColumns and REPEATDIRECTION properties, which are used to specify whether the data is

Display with multiple columns, as well as the direction (horizontal or vertical) of the priority (horizontal or vertical) DataList also supports style properties, such as

The following code:

Repeatcolumns = "2"

RepeatDirection = "horizontal"

ItemStyle-font-size = "10pt"

ItemStyle-font-name = "verdana"

>

...

Note: The remainder of this chapter focuses on many features of the DataList control. For more information on the REPEATER space, refer to the "Repeater" topic in the "Web Form Control Reference" section in this textbook.

The following example demonstrates the usage of the DataList control. Note that the appearance of the data item has been in and the previous example.

At the same, it has only changed the properties of the control ItemTemplate. RepeatDirection and RepeatColumns

The property determines the display style of ItemTemplates.

C # DataList2.aspx

[Run] | [Source Code]

The following example further demonstrates the colorful changes of the template by changing the itemtemplate. this time,

A value of DataITEM uses the tag's "src" attribute. Format string of DataBinder.eval

The number also replaces the value of DataItem in the URL query string.

C # DataList3.aspx

[Run] | [Source Code]

Processing the return in the template

Like DataGrid, you can activate a command from the DataList template, the template is bound to the DataList

It is self-passing the event handle. For example, LinkButton in iTemTemplate can activate the select command. Set

The DataList's onselectedIndexChanged property can call an event handle to respond to this command. please

Look at the following code:

= "Server">

The following example is a complete demonstration. In the MyDataList_Select event handle, there are several other

Server controls, to express details of selected items.

C # DataList4.aspx

[Run] | [Source Code]

Note that when DataList discriminates some specific commands such as SELECT and EDIT / UPDATE / CANCEL, in the template

Activated command strings can be any string. For all commands, DataList's onItemCommand

activation. Just like the above example, you can bind this event to the event handle.

View Ratings

View Discussions

Note that because not only one command can activate this event handle, you must use the selection statement to determine the specific

command. The following example demonstrates this situation

C # DataList5.aspx

[Run] | [Source Code]

Use selection and editing templates

In addition to using the page-level event handle to handle the select command, you can also use DataList to process this

event. If you define a SELECTEMTEMPLATE for DataList, DataList will be activated as SELECT

The data item of the command is implemented to implement this template. The following example uses SelectEdItemTemplate to make the selected book

The title font is bold.

C # DataList6.aspx

[Run] | [Source Code]

DataList also supports EditItemTemplate to edit the index value equal to the EditIndex properties of Datalist.

project. About the details of editing and updating work, refer to the "Update Data" master in the "Data Access" chapter in this textbook

question.

C # DataList7.aspx

[Run] | [Source Code]

Find controls in the template

Sometimes, we must find the controls contained in the template. If the ID number of the control is given in the template, then

You can achieve his related letters from his container (supporting the first control in the parent of InamingContainer).

interest. In this case, the container is a DataListItem control. Note that even if several controls have the same ID number (

There is a cycle advantage of DataList), each is also logically included in the namespace of the DataListITEM container control.

You can get DataListItem by traversing the DataList's items collection, then call DatalistItem's FI

The NDControl method (inherited from the Control base class) Retrieves the control with a specific ID number.

Save to favor

The following example demonstrates the technique just now.

C # DataList8.aspx

[Run] | [Source Code]

chapter summary

1.DataList and Repeater provide the display format of the developer's fine-tuning data binding list.

2. Use templates to express the display of binding data, such as HeadeRTemplate, FooterTemplate, or Itemte

Mplate.

3.Repeater control is a repetition of the general purpose, and there is nothing in his template, and it will not be displayed.

4.Datalist control provides more control over the layout and style of the project, which can format the output of its own performance code.

.

5.Datalist supports SELECT, EDIT / UPDATE / CANCEL, and Item Command events. Binding events

The handle to the Command event of the DataList can handle item command on the page level.

6.DataList supports both templates for SelectedItemTemplate and EditItemTemplate to control selection

During style and editing performance style.

7. Use the Control.FindControl method to get the controls in the template by programs. This method passes Datal

IST project set DataListItem call.

============ to be ========== ASP.NET Chinese professional network (www.ourasp.net) =============

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

New Post(0)