WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ASP.NET Web Forms - The Hashtable Object


The Hashtable article contains things in key/esteem pairs.


Create a Hashtable

The Hashtable article contains things in key/esteem sets. The keys are utilized as lists, and brisk scans can be made for qualities via looking through their keys.

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

The following code makes a Hashtable named mycountries and four components are added:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  diminish mycountries=New Hashtable
  mycountries.Add("N","Norway")
  mycountries.Add("S","Sweden")
  mycountries.Add("F","France")
  mycountries.Add("I","Italy")
end if
end sub
</script>

Data Binding

A Hashtable item may consequently produce the content and qualities to the accompanying controls:

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

To tie information to a RadioButtonList control, first make a RadioButtonList control (with no asp:ListItem components) in an .aspx page:

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True"/>
</form>

</body>
</html>

Then include the content that manufactures the list:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  diminish mycountries=New Hashtable
  mycountries.Add("N","Norway")
  mycountries.Add("S","Sweden")
  mycountries.Add("F","France")
  mycountries.Add("I","Italy")
  rb.DataSource=mycountries
  rb.DataValueField="Key"
  rb.DataTextField="Value"
  rb.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True"/>
</form>

</body>
</html>

Then we add a sub routine to be executed when the client taps on a thing in the RadioButtonList control. At the point when a radio catch is clicked, a content will show up in a label:

Example

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New Hashtable
  mycountries.Add("N","Norway")
  mycountries.Add("S","Sweden")
  mycountries.Add("F","France")
  mycountries.Add("I","Italy")
  rb.DataSource=mycountries
  rb.DataValueField="Key"
  rb.DataTextField="Value"
  rb.DataBind()
end if
end sub

sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your most loved nation is: " & rb.SelectedItem.Text
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage"/>
<p><asp:label id="lbl1" runat="server"/></p>
</form>

</body>
</html>
Show model »

Note: You can't pick the sort request of the things added to the Hashtable. To sort things one after another in order or numerically, utilize the SortedList object.