Using Templates in ASP.NET

ASP.NET provides a rich set of Server controls that you can use in your Web Forms pages. When you drag Server controls to a Web Forms page, the controls have a default set of properties and styles already applied to them.

These default properties and style provide a specific look and layout to the controls. You can customize the look and layout of the controls by setting properties and styles according to your requirements. However, there might be situations when you cannot customize the controls by using the properties and styles. In such situations, ASP.NET allows you to create templates for some Server controls. Templates are used to customize the look and layout of Server controls according to your requirements.

Templates consist of HTML elements, controls, and embedded Server controls that allow you to customize and manipulate the layout of a particular control. For example, you can customize the layout of the individual rows, alternating rows, or selected rows of a DataList or DataGrid ASP.NET Server control by defining different templates. Then, at run time, instead of the default HTML, the contents of the templates are rendered on the page.

You can define templates to control the layout of different portions — such as header item, footer item, edit item, selected item, alternating item, or separator — of a control according to your requirements. Different types of templates are available depending on the portion of the control for which the template is defined.

For example, the template defined for the header of a control is called HeaderTemplate. However, not all the Server controls support templates. The Server controls that support templates are Repeater, DataList, and DataGrid. Also, not all types of templates are supported by all the controls that support templates.

Again, you can manipulate the look and layout of individual controls by using styles; however, you should not confuse styles with templates. Styles are used to specify the appearance of controls by setting specific properties, such as color, font, and width.

On the other hand, templates are sets of HTML elements and controls that provide a specific layout to specific portions of a control. You can use styles with the controls that do not have templates defined for them. Also, you can use styles with the controls that have templates defined. For such controls, you can use styles to specify the appearance of the elements that define the template.

Before proceeding to create templates for the Repeater, DataList, and DataGrid Server controls, you need to understand the basic features of these controls, described next. Repeater control The Repeater control allows you to create custom lists to display data from different data sources.

Unlike other Server controls, such as TextBox and ListBox, the Repeater control does not have a default look and layout. You need to provide the layout to the Repeater control by creating templates. Because it is you who provides the layout to the control, you can create any kind of list for the control, depending on your requirements.

Some of the lists that you can create include tables or grids, comma-separated lists, and bulleted or numbered lists. After you drag a Repeater control on a form, you can set the base control properties by using the Properties window.

Then, you need to create templates to provide the layout to the control. Of all the templates supported by the Repeater control, you must create at least one template, ItemTemplate, to provide a basic layout to the control. Then, you can create other templates to enhance the look and layout of the control. To render a Repeater control on a page, you must bind the control to a data source and create ItemTemplate.

The Repeater control supports two events:

ItemCreated: Enables you to customize the way items are created. One way to customize the item-creation process is to set certain properties while the items are being created.

ItemCommand: Generated when users click one of the buttons included in the Repeater control items. These buttons could be the usual Button controls or the LinkButton controls.

DataList control

The DataList control enables you to display rows of data from a data source in a list. Each row of data displayed in the DataList control is an item. To create items, you must create at least one template, ItemTemplate. By default, the items in a DataList control are displayed in a single vertical column. However, you can provide a custom layout to the items in the control by using templates.

In addition to specifying the layout of controls and the data to be displayed in individual items, the DataList control enables you to specify how the individual items are laid out with respect to each other. The options that you can choose from are the following:

Vertical/horizontal: The default layout is a single vertical column. However, you can specify more than one column for the control. If you've specified a multiple-column layout for the control, you can also specify the ordering of items as vertical or horizontal.

Number of columns: You can specify the number of columns that the list will have irrespective of the ordering of items as vertical or horizontal. In this manner, you can control the rendered width of the Web page.

Flow/table: You can choose either flow layout or table layout for the control. The flow layout presents the list items in a format like a wordprocessing document, while the table layout presents the list items in an HTML table.

The DataList control supports many styles and templates that you can use extensively to customize the layout of the control. You can use the autoformat feature to customize the appearance of different elements of the control. In addition to the styles, you can create the templates to customize the control.

For example, you can create EditItemTemplate to allow item editing in the control. The DataList control also enables you to have a multiple-column layout. Also, you can customize the control to allow multiple-item selection.

In addition to the ItemCreated event, the DataList control supports five more events that are generated when buttons are clicked in the list items. The type of the event generated depends on the CommandName property of the button that is clicked. The DataGrid control has a generic event called ItemCommand. This event is generated when a user clicks a button that has no predefined command.

RELATED POST

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO


No comments:

Post a Comment