Reputation: 116
Iam belinda. now only i learn the asp.net controls.. I have tried the following code. But i got an error..
I have used a code to bind the hashtable with listbox but i cant.
Anyone please help me to understand the hashtable and the bind concept clearly...
.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Hashtable.aspx.vb" Inherits="Hashtable" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox><br />
</div>
</form>
</body>
</html>
.aspx.vb:
#Region "Namespaces"
Imports System.Data
Imports System.IO
Imports System.Net.Mail
#End Region
Partial Class Hashtable
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ht As New Hashtable
ht.Items.Add("1", "Sunday")
ht.Items.Add("2", "Monday")
ht.Items.Add("3", "Tuesday")
ht.Items.Add("4", "Wednesday")
ht.Items.Add("5", "Thursday")
ht.Items.Add("6", "Friday")
ht.Items.Add("7", "Saturday")
ListBox1.DataSource = ht
ListBox1.DataValueField = "Key"
ListBox1.DataTextField = "Value"
ListBox1.DataBind()
End Sub
End Class
Upvotes: 0
Views: 1021
Reputation: 1075
Don’t use the old Hashtable class, use the replacement Dictionary instead. The same is true for (almost) all classes from the System.Collections namespace.
though you can try
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ht As New Hashtable
ht.Items.Add("1", "Sunday")
ht.Items.Add("2", "Monday")
ht.Items.Add("3", "Tuesday")
ht.Items.Add("4", "Wednesday")
ht.Items.Add("5", "Thursday")
ht.Items.Add("6", "Friday")
ht.Items.Add("7", "Saturday")
foreach (var item in ht)
{
ListBox1.Items.Add(item.Value)
}
End Sub
Upvotes: 0
Reputation: 831
I think it is possible to databind to a hash table.
Something like this should work
Hashtable myHashTable = new HashTable();
myHashTable.Add ("First", "The first item");
myHashTable.Add ("Second", "The second item");
myHashTable.Add ("Third", "The third item");
ddlMyDropDownList.DataSource = myHashTable.Keys;
ddlMyDropDownList.DataBind();
In Your Code Here:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ht As Hashtable = New Hashtable(7)
ht.Add("1", "Sunday")
ht.Add("2", "Monday")
ht.Add("3", "Tuesday")
ht.Add("4", "Wednesday")
ht.Add("5", "Thursday")
ht.Add("6", "Friday")
ht.Add("7", "Saturday")
ListBox1.DataSource = ht
ListBox1.DataValueField = "Key"
ListBox1.DataTextField = "Value"
ListBox1.DataBind()
End Sub
End Class
Upvotes: 2