Using Templates in .NET

Drawing ANSI/AIM Code 128 in .NET Using Templates
Using Templates
Recognizing Code 128C In VS .NET
Using Barcode Control SDK for Visual Studio .NET Control to generate, create, read, scan barcode image in VS .NET applications.
Overview
Printing Code-128 In .NET
Using Barcode generation for VS .NET Control to generate, create Code 128 Code Set A image in Visual Studio .NET applications.
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 styles 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. This chapter introduces you to templates. You'll also learn how to create templates and combine them with the Repeater, DataList, and DataGrid controls.
Code 128 Code Set C Decoder In Visual Studio .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications.
Introduction to Templates
Generate Bar Code In .NET Framework
Using Barcode printer for Visual Studio .NET Control to generate, create bar code image in .NET framework applications.
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. Table 11-1 lists the different types of templates and the controls that support them. Table 11-1: Templates and the supporting controls Template ItemTemplate Description The set of elements and controls that are rendered once for each row in the data source. The set of elements and controls that are rendered once for every alternating row in the data source. The set of elements and controls that are rendered when an item is selected in the control. The set of elements and controls that are rendered when an item is in Edit mode. Supported by Repeater, DataList, and DataGrid
Barcode Reader In .NET Framework
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
AlternatingItemTemplate
Encoding USS Code 128 In C#.NET
Using Barcode generator for VS .NET Control to generate, create Code 128 Code Set A image in .NET framework applications.
Repeater and DataList
Code 128 Code Set C Printer In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create Code 128C image in ASP.NET applications.
SelectedItemTemplate
Code 128 Drawer In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create Code-128 image in Visual Studio .NET applications.
DataList
Encode USS Code 39 In Visual Studio .NET
Using Barcode generation for VS .NET Control to generate, create ANSI/AIM Code 39 image in Visual Studio .NET applications.
EditItemTemplate
Generate Barcode In VS .NET
Using Barcode maker for .NET framework Control to generate, create bar code image in .NET framework applications.
DataList and DataGrid
Code 128 Generation In .NET
Using Barcode creator for .NET Control to generate, create Code 128 Code Set A image in .NET framework applications.
Table 11-1: Templates and the supporting controls Template HeaderTemplate Description The set of elements and controls that are rendered once before all the items in the control. The set of elements and controls that are rendered once after all the items in the control. The set of elements that are rendered between each row. The set of elements and controls that are rendered while paging the information. Supported by Repeater, DataList, and DataGrid
Code 11 Creator In VS .NET
Using Barcode printer for .NET Control to generate, create Code11 image in .NET applications.
FooterTemplate
Drawing Code 3 Of 9 In Visual Studio .NET
Using Barcode creation for ASP.NET Control to generate, create Code39 image in ASP.NET applications.
Repeater, DataList, and DataGrid
GS1 128 Creator In Java
Using Barcode generation for Java Control to generate, create UCC-128 image in Java applications.
SeparatorTemplate
Encoding Barcode In C#
Using Barcode creator for .NET framework Control to generate, create barcode image in .NET framework applications.
Repeater and DataList
EAN-13 Supplement 5 Maker In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create GTIN - 13 image in ASP.NET applications.
PagerTemplate
Barcode Generation In .NET
Using Barcode drawer for ASP.NET Control to generate, create barcode image in ASP.NET applications.
DataGrid
Code 128 Maker In Java
Using Barcode printer for Java Control to generate, create Code 128 Code Set A image in Java applications.
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.
Generating Bar Code In Java
Using Barcode creation for Java Control to generate, create bar code image in Java applications.
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. Table 11-2 shows the different events that are generated. Table 11-2: The DataList events Command Edit Update Event EditCommand UpdateCommand
Barcode Generator In Visual C#.NET
Using Barcode encoder for .NET framework Control to generate, create barcode image in VS .NET applications.
Table 11-2: The DataList events Command Cancel Delete Caution Event CancelCommand DeleteCommand The CommandName property of a button is case-sensitive and takes values in lowercase. Therefore, be sure to assign lowercase values to the CommandName property.
DataGrid control The DataGrid Server control is a multicolumn, data-bound grid that enables you to define different types of columns. These columns not only provide layout to the grid contents, but also add functionalities to select, edit, sort, and page the data. Also, the DataGrid control provides functionality for full customization of the output through the use of specific columns called TemplateColumns. When you add a DataGrid control to a form, the control will be rendered only if it is bound to a data source. A data source can be created by using Visual Studio Data Designer or any database on a server. It can also be a simple structure, such as an array of type ArrayList. When you bind a DataGrid control to a data source, the columns are generated automatically based on the fields in the data source. However, you can specify the columns in different ways. The different types of columns that you can create are described as follows: Bound columns: Used to specify the database fields that need to be displayed in the columns along with the order, format, and style of the display. Hyperlink column: Used to display information as hyperlinks that users can click to navigate to different pages. For example, you can display product names as hyperlinks. When users click these hyperlinks, they are directed to product details in a separate page. Button columns: Used to display buttons for each item in the grid and add custom functionality to the buttons. For example, you can add a button labeled Query and associate logic with it. When users click the button, the logic is executed. Edit command columns: Used to allow in-place editing of items. Inplace editing allows users to edit the items in the grid only. To implement this editing, you need to add a special column in the grid that contains buttons labeled Edit. When users click this button, the current row is displayed again with editable fields for all columns. Also, the column with the Edit button is redisplayed with the Update and Cancel buttons. For more information on Button columns and Edit command Crosscolumns, refer to 10 and 12. Reference Template columns: Used to create a custom layout for a column by using a combination of HTML text and Server controls. The controls in the Template column can be bound to display data from a data source. This column provides you complete flexibility for customization. With the template columns, you can create custom-editing layout. This approach enables you to manipulate the columns that can be edited and the way users can edit data. The DataGrid control supports multiple events that are raised from the controls in the grids. In addition to the ItemCreated, ItemCommand, EditCommand, UpdateCommand, CancelCommand, and DeleteCommand events, the control supports these two events: PageIndexChanged: Generated when a page selection element is clicked. SortCommand: Generated when a column is sorted.
Comparing the Repeater, DataList, and DataGrid controls As mentioned, the Repeater, DataList, and DataGrid controls are used to display data in the form of lists on the Web Forms pages. All of these controls are bound to some data source and display each row in the data source as an entry called item. All three controls must be bound to a data source by the DataSource property so that they can be rendered on a page. Even if the DataSource property is set, the controls will not display the data from the data source until the DataBind() method is called for the control. The DataSource property is set for the control. For the individual controls in the templates, you can use the container data. CrossFor more information on data binding, refer to 9. Reference Table 11-3 compares the Repeater, DataList, and DataGrid controls. Table 11-3: Comparison of the Repeater, DataList, and DataGrid controls Feature Default layout Repeater No default layout DataList The default layout is a single vertical column layout Provides options for autoformat ting Provides options for editable contents and singleand multipleitem selection Does not support default paging DataGrid The default layout is a grid layout