SlideShare uma empresa 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>

Mais conteúdo relacionado

Mais procurados

MMYERS Portfolio
MMYERS PortfolioMMYERS Portfolio
MMYERS Portfolio
Mike Myers
 
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
Ammara Arooj
 
Parsing strange v3
Parsing strange v3Parsing strange v3
Parsing strange v3
Hal Stern
 
Diva10
Diva10Diva10
Diva10
diva23
 
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 net
Zishan yousaf
 
For Beginners - Ado.net
For Beginners - Ado.netFor Beginners - Ado.net
For Beginners - Ado.net
Tarun 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 vb
Amandeep Kaur
 
Prog1 chap1 and chap 2
Prog1 chap1 and chap 2Prog1 chap1 and chap 2
Prog1 chap1 and chap 2
rowensCap
 

Mais procurados (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
 

Semelhante a Asp.Net The Data List Control

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
 
Lecture 4 - Comm Lab: Web @ ITP
Lecture 4 - Comm Lab: Web @ ITPLecture 4 - Comm Lab: Web @ ITP
Lecture 4 - Comm Lab: Web @ ITP
yucefmerhi
 
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
isadorta
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
BG Java EE Course
 

Semelhante 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
 

Mais 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

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 

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>