SlideShare una empresa de Scribd logo
1 de 20
ASP.NET - Data Binding

We may use data binding to fill lists with selectable items from an imported data source,
like a database, an XML file, or a script.



Data Binding

The following controls are list controls which support data binding:


    •   asp:RadioButtonList
    •   asp:CheckBoxList
    •   asp:DropDownList
    •   asp:Listbox

The selectable items in each of the above controls are usually defined by one or more asp:ListItem
controls, like this:

<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;countrylistquot; runat=quot;serverquot;>
<asp:ListItem value=quot;Nquot; text=quot;Norwayquot; />
<asp:ListItem value=quot;Squot; text=quot;Swedenquot; />
<asp:ListItem value=quot;Fquot; text=quot;Francequot; />
<asp:ListItem value=quot;Iquot; text=quot;Italyquot; />
</asp:RadioButtonList>
</form>
</body>
</html>

However, with data binding we may use a separate source, like a database, an XML file, or a script
to fill the list with selectable items.

By using an imported source, the data is separated from the HTML, and any changes to the items
are made in the separate data source.

In the next three chapters, we will describe how to bind data from a scripted data source.
ASP.NET - The ArrayList Object

The ArrayList object is a collection of items containing a single data value.



Examples

Example 1 - ArrayList RadioButtonList

Example 2 - ArrayList DropDownList



Create an ArrayList

The ArrayList object is a collection of items containing a single data value.

Items are added to the ArrayList with the Add() method.

The following code creates a new ArrayList object named mycountries and four items are added:

<script runat=quot;serverquot;>
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add(quot;Norwayquot;)
  mycountries.Add(quot;Swedenquot;)
  mycountries.Add(quot;Francequot;)
  mycountries.Add(quot;Italyquot;)
end if
end sub
</script>

By default, an ArrayList object contains 16 entries. An ArrayList can be sized to its final size with the
TrimToSize() method:

<script runat=quot;serverquot;>
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add(quot;Norwayquot;)
  mycountries.Add(quot;Swedenquot;)
  mycountries.Add(quot;Francequot;)
  mycountries.Add(quot;Italyquot;)
  mycountries.TrimToSize()
end if
end sub
</script>

An ArrayList can also be sorted alphabetically or numerically with the Sort() method:

<script runat=quot;serverquot;>
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add(quot;Norwayquot;)
mycountries.Add(quot;Swedenquot;)
  mycountries.Add(quot;Francequot;)
  mycountries.Add(quot;Italyquot;)
  mycountries.TrimToSize()
  mycountries.Sort()
end if
end sub
</script>

To sort in reverse order, apply the Reverse() method after the Sort() method:

<script runat=quot;serverquot;>
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add(quot;Norwayquot;)
  mycountries.Add(quot;Swedenquot;)
  mycountries.Add(quot;Francequot;)
  mycountries.Add(quot;Italyquot;)
  mycountries.TrimToSize()
  mycountries.Sort()
  mycountries.Reverse()
end if
end sub
</script>


Data Binding to an ArrayList

An ArrayList object may automatically generate the text and values to the following controls:


    •   asp:RadioButtonList
    •   asp:CheckBoxList
    •   asp:DropDownList
    •   asp:Listbox

To bind data to a RadioButtonList control, first create a RadioButtonList control (without any
asp:ListItem elements) in an .aspx page:

<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; />
</form>
</body>
</html>

Then add the script that builds the list and binds the values in the list to the RadioButtonList
control:

<script runat=quot;serverquot;>
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add(quot;Norwayquot;)
  mycountries.Add(quot;Swedenquot;)
mycountries.Add(quot;Francequot;)
  mycountries.Add(quot;Italyquot;)
  mycountries.TrimToSize()
  mycountries.Sort()
  rb.DataSource=mycountries
  rb.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; />
</form>
</body>
</html>

The DataSource property of the RadioButtonList control is set to the ArrayList and it defines the
data source of the RadioButtonList control. The DataBind() method of the RadioButtonList control
binds the data source with the RadioButtonList control.

Note: The data values are used as both the Text and Value properties for the control. To add Values
that are different from the Text, use either the Hashtable object or the SortedList object.




ASP.NET - The Hashtable Object

The Hashtable object contains items in key/value pairs.
Examples

Example 1 - Hashtable RadioButtonList

Example 2 - Hashtable RadiobuttonList

Example 3 - Hashtable DropDownList



Create a Hashtable

The Hashtable object contains items in key/value pairs. The keys are used as indexes, and very
quick searches can be made for values by searching through their keys.

Items are added to the Hashtable with the Add() method.

The following code creates a Hashtable named mycountries and four elements are added:

<script runat=quot;serverquot;>
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New Hashtable
  mycountries.Add(quot;Nquot;,quot;Norwayquot;)
  mycountries.Add(quot;Squot;,quot;Swedenquot;)
  mycountries.Add(quot;Fquot;,quot;Francequot;)
  mycountries.Add(quot;Iquot;,quot;Italyquot;)
end if
end sub
</script>


Data Binding

A Hashtable object may automatically generate the text and values to the following controls:


    •   asp:RadioButtonList
    •   asp:CheckBoxList
    •   asp:DropDownList
    •   asp:Listbox

To bind data to a RadioButtonList control, first create a RadioButtonList control (without any
asp:ListItem elements) in an .aspx page:

<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; />
</form>
</body>
</html>

Then add the script that builds the list:

<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New Hashtable
  mycountries.Add(quot;Nquot;,quot;Norwayquot;)
  mycountries.Add(quot;Squot;,quot;Swedenquot;)
  mycountries.Add(quot;Fquot;,quot;Francequot;)
  mycountries.Add(quot;Iquot;,quot;Italyquot;)
  rb.DataSource=mycountries
  rb.DataValueField=quot;Keyquot;
  rb.DataTextField=quot;Valuequot;
  rb.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; />
</form>
</body>
</html>

Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList
control. When a radio button is clicked, a text will appear in a label:

<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New Hashtable
  mycountries.Add(quot;Nquot;,quot;Norwayquot;)
  mycountries.Add(quot;Squot;,quot;Swedenquot;)
  mycountries.Add(quot;Fquot;,quot;Francequot;)
  mycountries.Add(quot;Iquot;,quot;Italyquot;)
  rb.DataSource=mycountries
  rb.DataValueField=quot;Keyquot;
  rb.DataTextField=quot;Valuequot;
  rb.DataBind()
end if
end sub
sub displayMessage(s as Object,e As EventArgs)
lbl1.text=quot;Your favorite country is: quot; & rb.SelectedItem.Text
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; />
<p><asp:label id=quot;lbl1quot; runat=quot;serverquot; /></p>
</form>
</body>
</html>

Note: You cannot choose the sort order of the items added to the Hashtable. To sort items
alphabetically or numerically, use the SortedList object.
ASP.NET - The SortedList Object

The SortedList object combines the features of both the ArrayList object and the
Hashtable object.



Examples

Example 1 - SortedList RadioButtonList
Example 2 - SortedList RadiobuttonList

Example 3 - SortedList DropDownList



The SortedList Object

The SortedList object contains items in key/value pairs. A SortedList object automatically sort the
items in alphabetic or numeric order.

Items are added to the SortedList with the Add() method. A SortedList can be sized to its final size
with the TrimToSize() method.

The following code creates a SortedList named mycountries and four elements are added:

<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New SortedList
  mycountries.Add(quot;Nquot;,quot;Norwayquot;)
  mycountries.Add(quot;Squot;,quot;Swedenquot;)
  mycountries.Add(quot;Fquot;,quot;Francequot;)
  mycountries.Add(quot;Iquot;,quot;Italyquot;)
end if
end sub
</script>


Data Binding

A SortedList object may automatically generate the text and values to the following controls:


    •   asp:RadioButtonList
    •   asp:CheckBoxList
    •   asp:DropDownList
    •   asp:Listbox

To bind data to a RadioButtonList control, first create a RadioButtonList control (without any
asp:ListItem elements) in an .aspx page:

<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; />
</form>
</body>
</html>

Then add the script that builds the list:

<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New SortedList
  mycountries.Add(quot;Nquot;,quot;Norwayquot;)
mycountries.Add(quot;Squot;,quot;Swedenquot;)
  mycountries.Add(quot;Fquot;,quot;Francequot;)
  mycountries.Add(quot;Iquot;,quot;Italyquot;)
  rb.DataSource=mycountries
  rb.DataValueField=quot;Keyquot;
  rb.DataTextField=quot;Valuequot;
  rb.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; />
</form>
</body>
</html>

Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList
control. When a radio button is clicked, a text will appear in a label:

<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New SortedList
  mycountries.Add(quot;Nquot;,quot;Norwayquot;)
  mycountries.Add(quot;Squot;,quot;Swedenquot;)
  mycountries.Add(quot;Fquot;,quot;Francequot;)
  mycountries.Add(quot;Iquot;,quot;Italyquot;)
  rb.DataSource=mycountries
  rb.DataValueField=quot;Keyquot;
  rb.DataTextField=quot;Valuequot;
  rb.DataBind()
end if
end sub
sub displayMessage(s as Object,e As EventArgs)
lbl1.text=quot;Your favorite country is: quot; & rb.SelectedItem.Text
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; />
<p><asp:label id=quot;lbl1quot; runat=quot;serverquot; /></p>
</form>
</body>
</html>
ASP .NET - XML Files


We can bind an XML file to a list control.



Examples

Example 1 - XML RadiobuttonList



An XML File

Here is an XML file named quot;countries.xmlquot;:

<?xml version=quot;1.0quot; encoding=quot;ISO-8859-1quot;?>
<countries>
<country>
<text>Norway</text>
<value>N</value>
</country>
<country>
<text>Sweden</text>
<value>S</value>
</country>
<country>
<text>France</text>
<value>F</value>
</country>
<country>
<text>Italy</text>
<value>I</value>
</country>
</countries>




Bind a DataSet to a List Control

First, import the quot;System.Dataquot; namespace. We need this namespace to work with DataSet objects.
Include the following directive at the top of an .aspx page:

<%@ Import Namespace=quot;System.Dataquot; %>

Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first
loaded:

<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New DataSet
  mycountries.ReadXml(MapPath(quot;countries.xmlquot;))
end if
end sub

To bind the DataSet to a RadioButtonList control, first create a RadioButtonList control (without any
asp:ListItem elements) in an .aspx page:

<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; />
</form>
</body>
</html>

Then add the script that builds the XML DataSet:

<%@ Import Namespace=quot;System.Dataquot; %>
<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New DataSet
  mycountries.ReadXml(MapPath(quot;countries.xmlquot;))
  rb.DataSource=mycountries
  rb.DataValueField=quot;valuequot;
  rb.DataTextField=quot;textquot;
  rb.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; />
</form>
</body>
</html>

Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList
control. When a radio button is clicked, a text will appear in a label:

<%@ Import Namespace=quot;System.Dataquot; %>
<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New DataSet
  mycountries.ReadXml(MapPath(quot;countries.xmlquot;))
  rb.DataSource=mycountries
  rb.DataValueField=quot;valuequot;
  rb.DataTextField=quot;textquot;
  rb.DataBind()
end if
end sub
sub displayMessage(s as Object,e As EventArgs)
lbl1.text=quot;Your favorite country is: quot; & rb.SelectedItem.Text
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; />
<p><asp:label id=quot;lbl1quot; runat=quot;serverquot; /></p>
</form>
</body>
</html>
ASP .NET - XML Files

We can bind an XML file to a list control.



Examples

Example 1 - XML RadiobuttonList



An XML File

Here is an XML file named quot;countries.xmlquot;:

<?xml version=quot;1.0quot; encoding=quot;ISO-8859-1quot;?>
<countries>
<country>
<text>Norway</text>
<value>N</value>
</country>
<country>
<text>Sweden</text>
<value>S</value>
</country>
<country>
<text>France</text>
<value>F</value>
</country>
<country>
<text>Italy</text>
<value>I</value>
</country>
</countries>

Bind a DataSet to a List Control

First, import the quot;System.Dataquot; namespace. We need this namespace to work with DataSet objects.
Include the following directive at the top of an .aspx page:

<%@ Import Namespace=quot;System.Dataquot; %>

Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first
loaded:

<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New DataSet
  mycountries.ReadXml(MapPath(quot;countries.xmlquot;))
end if
end sub

To bind the DataSet to a RadioButtonList control, first create a RadioButtonList control (without any
asp:ListItem elements) in an .aspx page:

<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; />
</form>
</body>
</html>

Then add the script that builds the XML DataSet:

<%@ Import Namespace=quot;System.Dataquot; %>
<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New DataSet
  mycountries.ReadXml(MapPath(quot;countries.xmlquot;))
  rb.DataSource=mycountries
  rb.DataValueField=quot;valuequot;
  rb.DataTextField=quot;textquot;
  rb.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; />
</form>
</body>
</html>

Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList
control. When a radio button is clicked, a text will appear in a label:

<%@ Import Namespace=quot;System.Dataquot; %>
<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New DataSet
  mycountries.ReadXml(MapPath(quot;countries.xmlquot;))
  rb.DataSource=mycountries
  rb.DataValueField=quot;valuequot;
  rb.DataTextField=quot;textquot;
  rb.DataBind()
end if
end sub
sub displayMessage(s as Object,e As EventArgs)
lbl1.text=quot;Your favorite country is: quot; & rb.SelectedItem.Text
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot;
AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; />
<p><asp:label id=quot;lbl1quot; runat=quot;serverquot; /></p>
</form>
</body>
</html>
ASP.NET - The DataList Control

The DataList control is, like the Repeater control, used to display a repeated list of items
that are bound to the control. However, the DataList control adds a table around the data
items by default.



Examples

DataList control

DataList control with styles

DataList control with <AlternatingItemTemplate>



Bind a DataSet to a DataList Control

The DataList control is, like the Repeater control, used to display a repeated list of items that are
bound to the control. However, the DataList control adds a table around the data items by default.
The DataList control may be bound to a database table, an XML file, or another list of items. Here
we will show how to bind an XML file to a DataList control.

We will use the following XML file in our examples (quot;cdcatalog.xmlquot;):
<?xml version=quot;1.0quot; encoding=quot;ISO-8859-1quot;?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>

First, import the quot;System.Dataquot; namespace. We need this namespace to work with DataSet objects.
Include the following directive at the top of an .aspx page:

<%@ Import Namespace=quot;System.Dataquot; %>

Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first
loaded:

<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath(quot;cdcatalog.xmlquot;))
end if
end sub

Then we create a DataList in an .aspx page. The contents of the <HeaderTemplate> element are
rendered first and only once within the output, then the contents of the <ItemTemplate> element
are repeated for each quot;recordquot; in the DataSet, and last, the contents of the <FooterTemplate>
element are rendered once within the output:

<html>
<body>
<form runat=quot;serverquot;>
<asp:DataList id=quot;cdcatalogquot; runat=quot;serverquot;>
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

Then we add the script that creates the DataSet and binds the mycdcatalog DataSet to the DataList
control. We also fill the DataList control with a <HeaderTemplate> that contains the header of the
table, an <ItemTemplate> that contains the data items to display, and a <FooterTemplate> that
contains a text. Note that the gridlines attribute of the DataList is set to quot;bothquot; to display table
borders:

<%@ Import Namespace=quot;System.Dataquot; %>
<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath(quot;cdcatalog.xmlquot;))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:DataList id=quot;cdcatalogquot;
gridlines=quot;bothquot; runat=quot;serverquot;>
<HeaderTemplate>
My CD Catalog
</HeaderTemplate>
<ItemTemplate>
quot;<%#Container.DataItem(quot;titlequot;)%>quot; of
<%#Container.DataItem(quot;artistquot;)%> -
$<%#Container.DataItem(quot;pricequot;)%>
</ItemTemplate>
<FooterTemplate>
Copyright Hege Refsnes
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>


Using Styles

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

<%@ Import Namespace=quot;System.Dataquot; %>
<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath(quot;cdcatalog.xmlquot;))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:DataList id=quot;cdcatalogquot;
runat=quot;serverquot;
cellpadding=quot;2quot;
cellspacing=quot;2quot;
borderstyle=quot;insetquot;
backcolor=quot;#e8e8e8quot;
width=quot;100%quot;
headerstyle-font-name=quot;Verdanaquot;
headerstyle-font-size=quot;12ptquot;
headerstyle-horizontalalign=quot;centerquot;
headerstyle-font-bold=quot;truequot;
itemstyle-backcolor=quot;#778899quot;
itemstyle-forecolor=quot;#ffffffquot;
footerstyle-font-size=quot;9ptquot;
footerstyle-font-italic=quot;truequot;>
<HeaderTemplate>
My CD Catalog
</HeaderTemplate>
<ItemTemplate>
quot;<%#Container.DataItem(quot;titlequot;)%>quot; of
<%#Container.DataItem(quot;artistquot;)%> -
$<%#Container.DataItem(quot;pricequot;)%>
</ItemTemplate>
<FooterTemplate>
Copyright Hege Refsnes
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>


Using the <AlternatingItemTemplate>
You can add an <AlternatingItemTemplate> element after the <ItemTemplate> element to describe
the appearance of alternating rows of output. You may style the data in the
<AlternatingItemTemplate> section within the DataList control:

<%@ Import Namespace=quot;System.Dataquot; %>
<script runat=quot;serverquot;>
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath(quot;cdcatalog.xmlquot;))
cdcatalog.DataSource=mycdcatalog
cdcatalog.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat=quot;serverquot;>
<asp:DataList id=quot;cdcatalogquot;
runat=quot;serverquot;
cellpadding=quot;2quot;
cellspacing=quot;2quot;
borderstyle=quot;insetquot;
backcolor=quot;#e8e8e8quot;
width=quot;100%quot;
headerstyle-font-name=quot;Verdanaquot;
headerstyle-font-size=quot;12ptquot;
headerstyle-horizontalalign=quot;centerquot;
headerstyle-font-bold=quot;Truequot;
itemstyle-backcolor=quot;#778899quot;
itemstyle-forecolor=quot;#ffffffquot;
alternatingitemstyle-backcolor=quot;#e8e8e8quot;
alternatingitemstyle-forecolor=quot;#000000quot;
footerstyle-font-size=quot;9ptquot;
footerstyle-font-italic=quot;Truequot;>
<HeaderTemplate>
My CD Catalog
</HeaderTemplate>
<ItemTemplate>
quot;<%#Container.DataItem(quot;titlequot;)%>quot; of
<%#Container.DataItem(quot;artistquot;)%> -
$<%#Container.DataItem(quot;pricequot;)%>
</ItemTemplate>
<AlternatingItemTemplate>
quot;<%#Container.DataItem(quot;titlequot;)%>quot; of
<%#Container.DataItem(quot;artistquot;)%> -
$<%#Container.DataItem(quot;pricequot;)%>
</AlternatingItemTemplate>
<FooterTemplate>
&copy; Hege Refsnes
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

Más contenido relacionado

La actualidad más candente

MMYERS Portfolio
MMYERS PortfolioMMYERS Portfolio
MMYERS PortfolioMike Myers
 
Mimsy XG Resource Session
Mimsy XG Resource SessionMimsy XG Resource Session
Mimsy XG Resource SessionAxiell ALM
 
Web based database application design using vb.net and sql server
Web based database application design using vb.net and sql serverWeb based database application design using vb.net and sql server
Web based database application design using vb.net and sql serverAmmara Arooj
 
Parsing strange v3
Parsing strange v3Parsing strange v3
Parsing strange v3Hal Stern
 
Diva10
Diva10Diva10
Diva10diva23
 
Itemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integrationItemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integration{item:foo}
 
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 BookADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 BookMuralidharan Radhakrishnan
 
Load & Unload Data TO and FROM Snowflake (By Faysal Shaarani)
Load & Unload Data TO and FROM Snowflake (By Faysal Shaarani)Load & Unload Data TO and FROM Snowflake (By Faysal Shaarani)
Load & Unload Data TO and FROM Snowflake (By Faysal Shaarani)Faysal Shaarani (MBA)
 
Database programming in vb net
Database programming in vb netDatabase programming in vb net
Database programming in vb netZishan yousaf
 
Introduction to ADO.NET
Introduction to ADO.NETIntroduction to ADO.NET
Introduction to ADO.NETrchakra
 
For Beginners - Ado.net
For Beginners - Ado.netFor Beginners - Ado.net
For Beginners - Ado.netTarun Jain
 
Data base connectivity and flex grid in vb
Data base connectivity and flex grid in vbData base connectivity and flex grid in vb
Data base connectivity and flex grid in vbAmandeep Kaur
 
Base SAS Full Sample Paper
Base SAS Full Sample Paper Base SAS Full Sample Paper
Base SAS Full Sample Paper Jimmy Rana
 
Prog1 chap1 and chap 2
Prog1 chap1 and chap 2Prog1 chap1 and chap 2
Prog1 chap1 and chap 2rowensCap
 

La actualidad más candente (20)

ASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NETASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NET
 
MMYERS Portfolio
MMYERS PortfolioMMYERS Portfolio
MMYERS Portfolio
 
Mimsy XG Resource Session
Mimsy XG Resource SessionMimsy XG Resource Session
Mimsy XG Resource Session
 
VB6 Using ADO Data Control
VB6 Using ADO Data ControlVB6 Using ADO Data Control
VB6 Using ADO Data Control
 
Web based database application design using vb.net and sql server
Web based database application design using vb.net and sql serverWeb based database application design using vb.net and sql server
Web based database application design using vb.net and sql server
 
Ado .net
Ado .netAdo .net
Ado .net
 
Parsing strange v3
Parsing strange v3Parsing strange v3
Parsing strange v3
 
Ado.net
Ado.netAdo.net
Ado.net
 
Diva10
Diva10Diva10
Diva10
 
Grid Vew Control VB
Grid Vew Control VBGrid Vew Control VB
Grid Vew Control VB
 
Itemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integrationItemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integration
 
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 BookADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book
ADO Controls - Database Usage from Exploring MS Visual Basic 6.0 Book
 
Load & Unload Data TO and FROM Snowflake (By Faysal Shaarani)
Load & Unload Data TO and FROM Snowflake (By Faysal Shaarani)Load & Unload Data TO and FROM Snowflake (By Faysal Shaarani)
Load & Unload Data TO and FROM Snowflake (By Faysal Shaarani)
 
Database programming in vb net
Database programming in vb netDatabase programming in vb net
Database programming in vb net
 
Introduction to ADO.NET
Introduction to ADO.NETIntroduction to ADO.NET
Introduction to ADO.NET
 
For Beginners - Ado.net
For Beginners - Ado.netFor Beginners - Ado.net
For Beginners - Ado.net
 
Data base connectivity and flex grid in vb
Data base connectivity and flex grid in vbData base connectivity and flex grid in vb
Data base connectivity and flex grid in vb
 
Base SAS Full Sample Paper
Base SAS Full Sample Paper Base SAS Full Sample Paper
Base SAS Full Sample Paper
 
Prog1 chap1 and chap 2
Prog1 chap1 and chap 2Prog1 chap1 and chap 2
Prog1 chap1 and chap 2
 
SAS BASICS
SAS BASICSSAS BASICS
SAS BASICS
 

Similar a Asp.Net The Data List Control

ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationRandy Connolly
 
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...Roel Hartman
 
ASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET WorksASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET WorksRandy Connolly
 
Advanced dot net
Advanced dot netAdvanced dot net
Advanced dot netssa2010
 
Lecture 4 - Comm Lab: Web @ ITP
Lecture 4 - Comm Lab: Web @ ITPLecture 4 - Comm Lab: Web @ ITP
Lecture 4 - Comm Lab: Web @ ITPyucefmerhi
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQLkalaisai
 
Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9isadorta
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsBG Java EE Course
 
Skills Portfolio
Skills PortfolioSkills Portfolio
Skills Portfoliorolee23
 

Similar a Asp.Net The Data List Control (20)

Vb.Net Web Forms
Vb.Net  Web FormsVb.Net  Web Forms
Vb.Net Web Forms
 
ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And Representation
 
Controls
ControlsControls
Controls
 
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
 
Tugas Pw [6]
Tugas Pw [6]Tugas Pw [6]
Tugas Pw [6]
 
Tugas Pw [6] (2)
Tugas Pw [6] (2)Tugas Pw [6] (2)
Tugas Pw [6] (2)
 
Jquery 1
Jquery 1Jquery 1
Jquery 1
 
ASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET WorksASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET Works
 
Dat402
Dat402Dat402
Dat402
 
Advanced dot net
Advanced dot netAdvanced dot net
Advanced dot net
 
PHP MySQL
PHP MySQLPHP MySQL
PHP MySQL
 
Mysql
MysqlMysql
Mysql
 
Lecture 4 - Comm Lab: Web @ ITP
Lecture 4 - Comm Lab: Web @ ITPLecture 4 - Comm Lab: Web @ ITP
Lecture 4 - Comm Lab: Web @ ITP
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQL
 
Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
 
Skills Portfolio
Skills PortfolioSkills Portfolio
Skills Portfolio
 
Creating an RSS feed
Creating an RSS feedCreating an RSS feed
Creating an RSS feed
 
70 562
70 56270 562
70 562
 
Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
 

Más de Ram Sagar Mourya

Más de Ram Sagar Mourya (12)

A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
There Is Always A Better Way
There Is Always A Better WayThere Is Always A Better Way
There Is Always A Better Way
 
Attitude
AttitudeAttitude
Attitude
 
3 Things In Our Life
3 Things In Our Life3 Things In Our Life
3 Things In Our Life
 
Microsoft Word Shortcut Keys
Microsoft Word Shortcut KeysMicrosoft Word Shortcut Keys
Microsoft Word Shortcut Keys
 
Apple Macintosh Shortcut Keys
Apple Macintosh Shortcut KeysApple Macintosh Shortcut Keys
Apple Macintosh Shortcut Keys
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
Discrete Mathematics - Mathematics For Computer Science
Discrete Mathematics -  Mathematics For Computer ScienceDiscrete Mathematics -  Mathematics For Computer Science
Discrete Mathematics - Mathematics For Computer Science
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
.net framework
.net framework.net framework
.net framework
 
Asp.Net Tutorials
Asp.Net TutorialsAsp.Net Tutorials
Asp.Net Tutorials
 
Asp.Net Database
Asp.Net DatabaseAsp.Net Database
Asp.Net Database
 

Último

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Último (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Asp.Net The Data List Control

  • 1. ASP.NET - Data Binding We may use data binding to fill lists with selectable items from an imported data source, like a database, an XML file, or a script. Data Binding The following controls are list controls which support data binding: • asp:RadioButtonList • asp:CheckBoxList • asp:DropDownList • asp:Listbox The selectable items in each of the above controls are usually defined by one or more asp:ListItem controls, like this: <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;countrylistquot; runat=quot;serverquot;> <asp:ListItem value=quot;Nquot; text=quot;Norwayquot; /> <asp:ListItem value=quot;Squot; text=quot;Swedenquot; /> <asp:ListItem value=quot;Fquot; text=quot;Francequot; /> <asp:ListItem value=quot;Iquot; text=quot;Italyquot; /> </asp:RadioButtonList> </form> </body> </html> However, with data binding we may use a separate source, like a database, an XML file, or a script to fill the list with selectable items. By using an imported source, the data is separated from the HTML, and any changes to the items are made in the separate data source. In the next three chapters, we will describe how to bind data from a scripted data source.
  • 2. ASP.NET - The ArrayList Object The ArrayList object is a collection of items containing a single data value. Examples Example 1 - ArrayList RadioButtonList Example 2 - ArrayList DropDownList Create an ArrayList The ArrayList object is a collection of items containing a single data value. Items are added to the ArrayList with the Add() method. The following code creates a new ArrayList object named mycountries and four items are added: <script runat=quot;serverquot;> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add(quot;Norwayquot;) mycountries.Add(quot;Swedenquot;) mycountries.Add(quot;Francequot;) mycountries.Add(quot;Italyquot;) end if end sub </script> By default, an ArrayList object contains 16 entries. An ArrayList can be sized to its final size with the TrimToSize() method: <script runat=quot;serverquot;> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add(quot;Norwayquot;) mycountries.Add(quot;Swedenquot;) mycountries.Add(quot;Francequot;) mycountries.Add(quot;Italyquot;) mycountries.TrimToSize() end if end sub </script> An ArrayList can also be sorted alphabetically or numerically with the Sort() method: <script runat=quot;serverquot;> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add(quot;Norwayquot;)
  • 3. mycountries.Add(quot;Swedenquot;) mycountries.Add(quot;Francequot;) mycountries.Add(quot;Italyquot;) mycountries.TrimToSize() mycountries.Sort() end if end sub </script> To sort in reverse order, apply the Reverse() method after the Sort() method: <script runat=quot;serverquot;> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add(quot;Norwayquot;) mycountries.Add(quot;Swedenquot;) mycountries.Add(quot;Francequot;) mycountries.Add(quot;Italyquot;) mycountries.TrimToSize() mycountries.Sort() mycountries.Reverse() end if end sub </script> Data Binding to an ArrayList An ArrayList object may automatically generate the text and values to the following controls: • asp:RadioButtonList • asp:CheckBoxList • asp:DropDownList • asp:Listbox To bind data to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page: <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; /> </form> </body> </html> Then add the script that builds the list and binds the values in the list to the RadioButtonList control: <script runat=quot;serverquot;> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add(quot;Norwayquot;) mycountries.Add(quot;Swedenquot;)
  • 4. mycountries.Add(quot;Francequot;) mycountries.Add(quot;Italyquot;) mycountries.TrimToSize() mycountries.Sort() rb.DataSource=mycountries rb.DataBind() end if end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; /> </form> </body> </html> The DataSource property of the RadioButtonList control is set to the ArrayList and it defines the data source of the RadioButtonList control. The DataBind() method of the RadioButtonList control binds the data source with the RadioButtonList control. Note: The data values are used as both the Text and Value properties for the control. To add Values that are different from the Text, use either the Hashtable object or the SortedList object. ASP.NET - The Hashtable Object The Hashtable object contains items in key/value pairs.
  • 5. Examples Example 1 - Hashtable RadioButtonList Example 2 - Hashtable RadiobuttonList Example 3 - Hashtable DropDownList Create a Hashtable The Hashtable object contains items in key/value pairs. The keys are used as indexes, and very quick searches can be made for values by searching through their keys. Items are added to the Hashtable with the Add() method. The following code creates a Hashtable named mycountries and four elements are added: <script runat=quot;serverquot;> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New Hashtable mycountries.Add(quot;Nquot;,quot;Norwayquot;) mycountries.Add(quot;Squot;,quot;Swedenquot;) mycountries.Add(quot;Fquot;,quot;Francequot;) mycountries.Add(quot;Iquot;,quot;Italyquot;) end if end sub </script> Data Binding A Hashtable object may automatically generate the text and values to the following controls: • asp:RadioButtonList • asp:CheckBoxList • asp:DropDownList • asp:Listbox To bind data to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page: <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; /> </form> </body> </html> Then add the script that builds the list: <script runat=quot;serverquot;>
  • 6. sub Page_Load if Not Page.IsPostBack then dim mycountries=New Hashtable mycountries.Add(quot;Nquot;,quot;Norwayquot;) mycountries.Add(quot;Squot;,quot;Swedenquot;) mycountries.Add(quot;Fquot;,quot;Francequot;) mycountries.Add(quot;Iquot;,quot;Italyquot;) rb.DataSource=mycountries rb.DataValueField=quot;Keyquot; rb.DataTextField=quot;Valuequot; rb.DataBind() end if end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; /> </form> </body> </html> Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList control. When a radio button is clicked, a text will appear in a label: <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycountries=New Hashtable mycountries.Add(quot;Nquot;,quot;Norwayquot;) mycountries.Add(quot;Squot;,quot;Swedenquot;) mycountries.Add(quot;Fquot;,quot;Francequot;) mycountries.Add(quot;Iquot;,quot;Italyquot;) rb.DataSource=mycountries rb.DataValueField=quot;Keyquot; rb.DataTextField=quot;Valuequot; rb.DataBind() end if end sub sub displayMessage(s as Object,e As EventArgs) lbl1.text=quot;Your favorite country is: quot; & rb.SelectedItem.Text end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; /> <p><asp:label id=quot;lbl1quot; runat=quot;serverquot; /></p> </form> </body> </html> Note: You cannot choose the sort order of the items added to the Hashtable. To sort items alphabetically or numerically, use the SortedList object.
  • 7. ASP.NET - The SortedList Object The SortedList object combines the features of both the ArrayList object and the Hashtable object. Examples Example 1 - SortedList RadioButtonList
  • 8. Example 2 - SortedList RadiobuttonList Example 3 - SortedList DropDownList The SortedList Object The SortedList object contains items in key/value pairs. A SortedList object automatically sort the items in alphabetic or numeric order. Items are added to the SortedList with the Add() method. A SortedList can be sized to its final size with the TrimToSize() method. The following code creates a SortedList named mycountries and four elements are added: <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add(quot;Nquot;,quot;Norwayquot;) mycountries.Add(quot;Squot;,quot;Swedenquot;) mycountries.Add(quot;Fquot;,quot;Francequot;) mycountries.Add(quot;Iquot;,quot;Italyquot;) end if end sub </script> Data Binding A SortedList object may automatically generate the text and values to the following controls: • asp:RadioButtonList • asp:CheckBoxList • asp:DropDownList • asp:Listbox To bind data to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page: <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; /> </form> </body> </html> Then add the script that builds the list: <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add(quot;Nquot;,quot;Norwayquot;)
  • 9. mycountries.Add(quot;Squot;,quot;Swedenquot;) mycountries.Add(quot;Fquot;,quot;Francequot;) mycountries.Add(quot;Iquot;,quot;Italyquot;) rb.DataSource=mycountries rb.DataValueField=quot;Keyquot; rb.DataTextField=quot;Valuequot; rb.DataBind() end if end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; /> </form> </body> </html> Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList control. When a radio button is clicked, a text will appear in a label: <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add(quot;Nquot;,quot;Norwayquot;) mycountries.Add(quot;Squot;,quot;Swedenquot;) mycountries.Add(quot;Fquot;,quot;Francequot;) mycountries.Add(quot;Iquot;,quot;Italyquot;) rb.DataSource=mycountries rb.DataValueField=quot;Keyquot; rb.DataTextField=quot;Valuequot; rb.DataBind() end if end sub sub displayMessage(s as Object,e As EventArgs) lbl1.text=quot;Your favorite country is: quot; & rb.SelectedItem.Text end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; /> <p><asp:label id=quot;lbl1quot; runat=quot;serverquot; /></p> </form> </body> </html>
  • 10. ASP .NET - XML Files We can bind an XML file to a list control. Examples Example 1 - XML RadiobuttonList An XML File Here is an XML file named quot;countries.xmlquot;: <?xml version=quot;1.0quot; encoding=quot;ISO-8859-1quot;?>
  • 11. <countries> <country> <text>Norway</text> <value>N</value> </country> <country> <text>Sweden</text> <value>S</value> </country> <country> <text>France</text> <value>F</value> </country> <country> <text>Italy</text> <value>I</value> </country> </countries> Bind a DataSet to a List Control First, import the quot;System.Dataquot; namespace. We need this namespace to work with DataSet objects. Include the following directive at the top of an .aspx page: <%@ Import Namespace=quot;System.Dataquot; %> Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded: <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycountries=New DataSet mycountries.ReadXml(MapPath(quot;countries.xmlquot;)) end if end sub To bind the DataSet to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page: <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; /> </form> </body> </html> Then add the script that builds the XML DataSet: <%@ Import Namespace=quot;System.Dataquot; %> <script runat=quot;serverquot;>
  • 12. sub Page_Load if Not Page.IsPostBack then dim mycountries=New DataSet mycountries.ReadXml(MapPath(quot;countries.xmlquot;)) rb.DataSource=mycountries rb.DataValueField=quot;valuequot; rb.DataTextField=quot;textquot; rb.DataBind() end if end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; /> </form> </body> </html> Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList control. When a radio button is clicked, a text will appear in a label: <%@ Import Namespace=quot;System.Dataquot; %> <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycountries=New DataSet mycountries.ReadXml(MapPath(quot;countries.xmlquot;)) rb.DataSource=mycountries rb.DataValueField=quot;valuequot; rb.DataTextField=quot;textquot; rb.DataBind() end if end sub sub displayMessage(s as Object,e As EventArgs) lbl1.text=quot;Your favorite country is: quot; & rb.SelectedItem.Text end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; /> <p><asp:label id=quot;lbl1quot; runat=quot;serverquot; /></p> </form> </body> </html>
  • 13. ASP .NET - XML Files We can bind an XML file to a list control. Examples Example 1 - XML RadiobuttonList An XML File Here is an XML file named quot;countries.xmlquot;: <?xml version=quot;1.0quot; encoding=quot;ISO-8859-1quot;?> <countries> <country> <text>Norway</text> <value>N</value> </country> <country>
  • 14. <text>Sweden</text> <value>S</value> </country> <country> <text>France</text> <value>F</value> </country> <country> <text>Italy</text> <value>I</value> </country> </countries> Bind a DataSet to a List Control First, import the quot;System.Dataquot; namespace. We need this namespace to work with DataSet objects. Include the following directive at the top of an .aspx page: <%@ Import Namespace=quot;System.Dataquot; %> Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded: <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycountries=New DataSet mycountries.ReadXml(MapPath(quot;countries.xmlquot;)) end if end sub To bind the DataSet to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page: <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; /> </form> </body> </html> Then add the script that builds the XML DataSet: <%@ Import Namespace=quot;System.Dataquot; %> <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycountries=New DataSet mycountries.ReadXml(MapPath(quot;countries.xmlquot;)) rb.DataSource=mycountries rb.DataValueField=quot;valuequot; rb.DataTextField=quot;textquot; rb.DataBind() end if end sub
  • 15. </script> <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; /> </form> </body> </html> Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList control. When a radio button is clicked, a text will appear in a label: <%@ Import Namespace=quot;System.Dataquot; %> <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycountries=New DataSet mycountries.ReadXml(MapPath(quot;countries.xmlquot;)) rb.DataSource=mycountries rb.DataValueField=quot;valuequot; rb.DataTextField=quot;textquot; rb.DataBind() end if end sub sub displayMessage(s as Object,e As EventArgs) lbl1.text=quot;Your favorite country is: quot; & rb.SelectedItem.Text end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:RadioButtonList id=quot;rbquot; runat=quot;serverquot; AutoPostBack=quot;Truequot; onSelectedIndexChanged=quot;displayMessagequot; /> <p><asp:label id=quot;lbl1quot; runat=quot;serverquot; /></p> </form> </body> </html>
  • 16. ASP.NET - The DataList Control The DataList control is, like the Repeater control, used to display a repeated list of items that are bound to the control. However, the DataList control adds a table around the data items by default. Examples DataList control DataList control with styles DataList control with <AlternatingItemTemplate> Bind a DataSet to a DataList Control The DataList control is, like the Repeater control, used to display a repeated list of items that are bound to the control. However, the DataList control adds a table around the data items by default. The DataList control may be bound to a database table, an XML file, or another list of items. Here we will show how to bind an XML file to a DataList control. We will use the following XML file in our examples (quot;cdcatalog.xmlquot;):
  • 17. <?xml version=quot;1.0quot; encoding=quot;ISO-8859-1quot;?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd> <cd> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <country>USA</country> <company>RCA</company> <price>9.90</price> <year>1982</year> </cd> <cd> <title>Still got the blues</title> <artist>Gary Moore</artist> <country>UK</country> <company>Virgin records</company> <price>10.20</price> <year>1990</year> </cd> <cd> <title>Eros</title> <artist>Eros Ramazzotti</artist> <country>EU</country> <company>BMG</company> <price>9.90</price> <year>1997</year> </cd> </catalog> First, import the quot;System.Dataquot; namespace. We need this namespace to work with DataSet objects. Include the following directive at the top of an .aspx page: <%@ Import Namespace=quot;System.Dataquot; %> Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded: <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath(quot;cdcatalog.xmlquot;)) end if
  • 18. end sub Then we create a DataList in an .aspx page. The contents of the <HeaderTemplate> element are rendered first and only once within the output, then the contents of the <ItemTemplate> element are repeated for each quot;recordquot; in the DataSet, and last, the contents of the <FooterTemplate> element are rendered once within the output: <html> <body> <form runat=quot;serverquot;> <asp:DataList id=quot;cdcatalogquot; runat=quot;serverquot;> <HeaderTemplate> ... </HeaderTemplate> <ItemTemplate> ... </ItemTemplate> <FooterTemplate> ... </FooterTemplate> </asp:DataList> </form> </body> </html> Then we add the script that creates the DataSet and binds the mycdcatalog DataSet to the DataList control. We also fill the DataList control with a <HeaderTemplate> that contains the header of the table, an <ItemTemplate> that contains the data items to display, and a <FooterTemplate> that contains a text. Note that the gridlines attribute of the DataList is set to quot;bothquot; to display table borders: <%@ Import Namespace=quot;System.Dataquot; %> <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath(quot;cdcatalog.xmlquot;)) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind() end if end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:DataList id=quot;cdcatalogquot; gridlines=quot;bothquot; runat=quot;serverquot;> <HeaderTemplate> My CD Catalog </HeaderTemplate> <ItemTemplate> quot;<%#Container.DataItem(quot;titlequot;)%>quot; of <%#Container.DataItem(quot;artistquot;)%> - $<%#Container.DataItem(quot;pricequot;)%> </ItemTemplate> <FooterTemplate> Copyright Hege Refsnes </FooterTemplate>
  • 19. </asp:DataList> </form> </body> </html> Using Styles You can also add styles to the DataList control to make the output more fancy: <%@ Import Namespace=quot;System.Dataquot; %> <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath(quot;cdcatalog.xmlquot;)) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind() end if end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:DataList id=quot;cdcatalogquot; runat=quot;serverquot; cellpadding=quot;2quot; cellspacing=quot;2quot; borderstyle=quot;insetquot; backcolor=quot;#e8e8e8quot; width=quot;100%quot; headerstyle-font-name=quot;Verdanaquot; headerstyle-font-size=quot;12ptquot; headerstyle-horizontalalign=quot;centerquot; headerstyle-font-bold=quot;truequot; itemstyle-backcolor=quot;#778899quot; itemstyle-forecolor=quot;#ffffffquot; footerstyle-font-size=quot;9ptquot; footerstyle-font-italic=quot;truequot;> <HeaderTemplate> My CD Catalog </HeaderTemplate> <ItemTemplate> quot;<%#Container.DataItem(quot;titlequot;)%>quot; of <%#Container.DataItem(quot;artistquot;)%> - $<%#Container.DataItem(quot;pricequot;)%> </ItemTemplate> <FooterTemplate> Copyright Hege Refsnes </FooterTemplate> </asp:DataList> </form> </body> </html> Using the <AlternatingItemTemplate>
  • 20. You can add an <AlternatingItemTemplate> element after the <ItemTemplate> element to describe the appearance of alternating rows of output. You may style the data in the <AlternatingItemTemplate> section within the DataList control: <%@ Import Namespace=quot;System.Dataquot; %> <script runat=quot;serverquot;> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath(quot;cdcatalog.xmlquot;)) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind() end if end sub </script> <html> <body> <form runat=quot;serverquot;> <asp:DataList id=quot;cdcatalogquot; runat=quot;serverquot; cellpadding=quot;2quot; cellspacing=quot;2quot; borderstyle=quot;insetquot; backcolor=quot;#e8e8e8quot; width=quot;100%quot; headerstyle-font-name=quot;Verdanaquot; headerstyle-font-size=quot;12ptquot; headerstyle-horizontalalign=quot;centerquot; headerstyle-font-bold=quot;Truequot; itemstyle-backcolor=quot;#778899quot; itemstyle-forecolor=quot;#ffffffquot; alternatingitemstyle-backcolor=quot;#e8e8e8quot; alternatingitemstyle-forecolor=quot;#000000quot; footerstyle-font-size=quot;9ptquot; footerstyle-font-italic=quot;Truequot;> <HeaderTemplate> My CD Catalog </HeaderTemplate> <ItemTemplate> quot;<%#Container.DataItem(quot;titlequot;)%>quot; of <%#Container.DataItem(quot;artistquot;)%> - $<%#Container.DataItem(quot;pricequot;)%> </ItemTemplate> <AlternatingItemTemplate> quot;<%#Container.DataItem(quot;titlequot;)%>quot; of <%#Container.DataItem(quot;artistquot;)%> - $<%#Container.DataItem(quot;pricequot;)%> </AlternatingItemTemplate> <FooterTemplate> &copy; Hege Refsnes </FooterTemplate> </asp:DataList> </form> </body> </html>