ASP.NET - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ASP.NET Hashtable

ASP.NET Hashtable

shape Description

The ASP.NET Hashtable is also a collection of items containing a single data value with key. Here, the use of key is the specialty for Hashtable. This key is used as index for searching of value in a very fast way. Like ArrayList, even here one can use ‘Add()’ method to add items to the table. [csharp] Hashtable spHashtable = new Hashtable(); spHashtable.Add("1", "Sunday"); spHashtable.Add("2", "Monday"); spHashtable.Add("3", "Tuesday"); spHashtable.Add("4", "Wednesday"); spHashtable.Add("5", "Thursday"); spHashtable.Add("6", "Friday"); spHashtable.Add("7", "Saturday"); [/csharp] In the above code, one can see that 7 days of a week have been added to the Hashtable using ‘Add()’ method. Hashtable can also be used as a data source for data binding controls like RadioButtonList, CheckBoxList etc.

shape Example

In the below example, one can see on clicking a button a CheckBoxList is populated and the data source for this CheckBoxList is a Hashtable. In designer, one Button control and one CheckBoxList has been added. In ‘Default.aspx.cs’ page, under ‘spButton_Click’ event the code has been written to add days to the spHashtable using ‘Add()’ method. [csharp] protected void spButton_Click(object sender, EventArgs e) { Hashtable spHashtable = new Hashtable(); spHashtable.Add("1", "Sunday"); spHashtable.Add("2", "Monday"); spHashtable.Add("3", "Tuesday"); spHashtable.Add("4", "Wednesday"); spHashtable.Add("5", "Thursday"); spHashtable.Add("6", "Friday"); spHashtable.Add("7", "Saturday"); spCheckBoxList.DataSource = spHashtable; spCheckBoxList.DataValueField = "key"; spCheckBoxList.DataTextField = "value"; spCheckBoxList.DataBind(); } [/csharp] When viewed the aspx in browser, one will get the following screens. At first only the button will appear in the page. On clicking the button a CheckBoxList will be shown with the days. After clicking the button. below, page will appear.