YouTip LogoYouTip

Aspnet Datalist

ASP.NET DataList Control

ASP.NET Web Forms - DataList Control


The DataList control, similar to the Repeater control, is used to display a repeated list of items bound to the control. However, the DataList control will automatically add tables around data items by default.

Binding DataSet to DataList Control

The DataList control, similar to the Repeater control, is used to display a repeated list of items bound to the control. However, the DataList control will automatically add tables around data items by default. The DataList control can be bound to database tables, XML files, or other list items. Here we will demonstrate how to bind an XML file to the DataList control. In our example, we will use the following XML file ("cdcatalog.xml"): Empire Burlesque Bob Dylan USA Columbia 10.90 1985 Hide your heart Bonnie Tyler UK CBS Records 9.90 1988 Greatest Hits Dolly Parton USA RCA 9.90 1982 Still got the blues Gary Moore UK Virgin records 10.20 1990 Eros Eros Ramazzotti EU BMG 9.90 1997 View this XML file: cdcatalog.xml First, import the "System.Data" namespace. We need this namespace to work with DataSet objects. Include the following directive at the top of your .aspx page: Next, create a DataSet for the XML file and load this XML file into the DataSet when the page first loads: sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) end if end sub Then we create a DataList control in the .aspx page. The content within the element will be rendered first and appear only once in the output. The content within the element will be repeated for each "record" in the DataSet. Finally, the content within the element will appear only once in the output: ... ... ... Then we add the script to create the DataSet and bind the mycdcatalog DataSet to the DataList control. We then use containing headers, containing data items to display, and containing text to populate the DataList control. Note that the DataList's gridlines property can be set to "both" to display table borders:

Example

sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind() end if end sub My CD Catalog "" of - $ Copyright Hege Refsnes Run Example Β»

Using Styles

You can also add styles to the DataList control to make the output more visually appealing:

Example

sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind() end if end sub My CD Catalog "" of - $ Copyright Hege Refsnes Run Example Β»

Using <AlternatingItemTemplate>

You can add an element after the element to define the appearance of alternating rows in the output. You can add styles to the section within the DataList control:

Example

sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind() end if end sub My CD Catalog "" of - $ "" of - $ © Hege Refsnes Run Example Β»
← Aspnet ExamplesAspnet Repeater β†’