1.6. Displaying Data from a Hashtable
Problem
You have data in a Hashtable, a class that
provides
the ability to store a collection of key/value pairs, and you want to
display the data in a columnar table.
Solution
Use a DataList control
and bind the
Hashtable to it.
Add a DataList control to the
.aspx file, being careful to place it in a Table
cell in order to control its position on the form.
In the code-behind class for the page, use the .NET language of your choice to:
Define the
Hashtableas the data source for theDataListcontrol.Set the control’s key and value.
Bind the
Hashtableto theDataListcontrol.
Figure 1-5 shows the appearance of a typical
DataList within a browser that has been bound to a
Hashtable filled with, in our case, book data.
Example 1-14 through Example 1-16 show the .aspx and
code-behind files for an application that produces this result.

Figure 1-5. DataList with Hashtable data output
Discussion
The DataList control can display almost any type
of data in a variety of ways using its available templates and
styles. Templates are available for the header, footer, items,
alternating items, separators, selected items, and edit items to
define and organize the data to output. Styles are available for each
of the templates to define how the content appears.
In this example, an asp:DataList tag is placed in a Table cell to control its position on the form, as shown ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access