ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
ASP Dot Net Tutorial - Index
ASP Dot Net Tutorial - Microsofts ASP.NET
ASP Dot Net Tutorial - Begining Web Forms
ASP Dot Net Tutorial - User Controls
ASP Dot Net Tutorial - ADO .NET
ASP Dot Net Tutorial - DataBinding to WebControls
ASP Dot Net Tutorial - DataList Controls
ASP Dot Net Tutorial - DataGrid Control
ASP Dot Net Tutorial - Advance Practical Examples on DataGrid
ASP Dot Net Tutorial - Working with DataSets
ASP Dot Net Tutorial - Working with XML
ASP Dot Net Tutorial - Forms Based Authentication
ASP Dot Net Tutorial - Windows Based Authentication
     

 
Online Personal Trainer Certification
Test Search by Job Title
Find a Personal Trainer
Job Resources
Free Self Tests and Quizzes
Find a Lawyer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Six Sigma Green Belt Certification
Lean Management Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
SEO Certification
Online Photography Course & Certification
Online Business Writing Course & Certification
Baby Sitting Course & Certification
Time Manangement Certification
Health Club Management Certification
Selling Skills Certification
Business Analysis Course
Green Living Course  free!
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > ASP.NET Tutorial > The DataList Controls
 

ASP.NET Tutorial - The DataList Controls

                                                                                                                Page 3 of 3
 

Using Templates with a DataList

Templates can be used with a DataList, to control the formatting of displayed items.

For example, suppose you want to display multiple database fields for each item displayed by the DataList. You can format the appearance of each DataList item by placing an HTML table in the ItemTemplate.

Example 43 DataListTemplateDemo.aspx

<%@ Import Namespace="System.Data.SqlClient" %>

<Script Runat="Server">

Sub Page_Load

Dim conNorthwind As SqlConnection

Dim cmdSelect As SqlCommand

Dim dtrEmployees As SqlDataReader

conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Northwind" )

cmdSelect = New SqlCommand( "Select * From Employees", conNorthwind )

conNorthwind.Open()

dtrEmployees = cmdSelect.ExecuteReader()

dlstEmployees.DataSource = dtrEmployees

dlstEmployees.DataBind()

dtrEmployees.Close()

conNorthwind.Close()

End Sub

</Script>

<html>

<head><title>ExpertDataListTemplateDemo.aspx</title></head>

<body>

<form Runat="Server">

 <asp:DataList

ID="dlstEmployees"

Runat="Server">

 <HeaderTemplate>

<h3>Employee's Home Phone Numbers:</h3>

</HeaderTemplate>

 <ItemTemplate>

<table>

<tr>

<td align="right">First Name:</td>

<td><%# Container.DataItem( "FirstName" )%></td>

</tr>

<tr>

<td align="right">Last Name:</td>

<td><%# Container.DataItem( "LastName" )%></td>

</tr>

<tr>

<td align="right">Home Phone:</td>

<td><%# Container.DataItem( "Homephone" )%></td>

</tr>

</table>

</ItemTemplate>

 <SeparatorTemplate>

<hr>

</SeparatorTemplate>

 </asp:DataList>

 </form>

</body>

</html>

 

The output of the above example is shown below:

In the above example three templates are used:- an ItemTemplate, aHeaderTemplate and a SeparatorTemplate.

  • HeaderTemplate displays the heading Employee Home phone numbers at the top of DataList.
  • The ItemTemplate displays each author’s first name, last name and home phone number.
  • The SeperatorTemplate renders a horizontal rule beween each item(just like <HR> tag of HTML)

Binding Data to the Control

As discussed earlier that to bind data to control, the DataList control must be bound to a data source. Typical data sources for the DataList are data sets or Data View and data readers (OleDbDataReaderor SqlDataReader objects). Binding can also be done to data sources created in code, such as a data reader or an array. The control can be used as a data source and any class that supports the IEnumerable interface.

When a data is bounded, a data source is specified for the DataList control as a whole. When other controls are added to the control — for example, labels or text boxes in list items — they are in turn bounded to the container, namely the DataList control.

       

 

 

 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2006. All Rights Reserved.