ASP.Net/Collections/Hashtable

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Define and use Hashtable (VB.net)

   <source lang="csharp">

<%@ Page Language="VB" %> <script runat="server">

   Dim ColorList(6) as String
   Dim FontList as new ArrayList()
   Dim QuoteList as new Hashtable()
   
   Sub Page_Load
     ColorList(0) = "Red"
     ColorList(1) = "Orange"
     ColorList(2) = "Yellow"
     ColorList(3) = "Green"
     ColorList(4) = "Blue"
     ColorList(5) = "Indigo"
     ColorList(6) = "Violet"
   
     FontList.Add("Times New Roman")
     FontList.Add("Arial")
     FontList.Add("Verdana")
     FontList.Add("Comic Sans MS")
     
     QuoteList.Add ("Quotation", "Quotation. ")
     QuoteList.Add ("Joke", "Joke")
     QuoteList.Add ("Wisdom", "Wisdom")
     QuoteList.Add ("Saying", "Saying")
   
     If Not Page.IsPostback
       Dim ColorName as String
   
       For Each ColorName in ColorList
         ddlColorList.Items.Add(ColorName)
       Next
   
       ddlFontList.DataSource = FontList
       ddlFontList.DataBind()
       
       ddlQuoteList.DataSource = QuoteList.Keys
       ddlQuoteList.DataBind()
   
     End If
   End Sub
   
   Sub btnSelectColor_Click(sender As Object, e As EventArgs)
     lblOutputMessage.Text = QuoteList(ddlQuoteList.SelectedItem.Text)
     lblOutputMessage.ForeColor = _
       System.Drawing.Color.FromName(ddlColorList.SelectedItem.Text)
     lblOutputMessage.Font.Name = _
       ddlFontList.SelectedItem.Text
   
   End Sub

</script> <html> <head> </head> <body>

   <form runat="server">

Select a color from the list:<asp:DropDownList id="ddlColorList" runat="server"></asp:DropDownList>

Then select a font sytle from the list: <asp:DropDownList id="ddlFontList" runat="server"></asp:DropDownList>

Finally, choose your output message: <asp:DropDownList id="ddlQuoteList" runat="server"></asp:DropDownList>

 <asp:Button id="btnSelectColor" onclick="btnSelectColor_Click" runat="server" Text="Click here!"></asp:Button>

<asp:Label id="lblOutputMessage" runat="server"></asp:Label>

   </form>

</body> </html>

      </source>
   
  


For Each loop through a Hashtable (VB.net)

   <source lang="csharp">

<%@Page language="vb" debug="true"  %> <script runat="server" language="vb">

 Sub Page_Load(Source As Object, E as EventArgs)
   Dim myHashTable as new Hashtable
   Dim Item As DictionaryEntry
   myHashTable("UK") = "United Kingdom"
   myHashTable("US") = "United States"
   myHashTable("DE") = "Germany"
   If Not Page.IsPostback Then
     For Each Item In myHashtable
       Dim newListItem As new ListItem()
       newListItem.Text = Item.Value
       newListItem.Value = Item.Key
       myDropDownList.Items.Add(newListItem)
     Next
   End If 
 End Sub
 Sub Click(Source As Object, E as EventArgs)
   myLabel.Text = myDropDownList.SelectedItem.Value
 End Sub

</script> <html>

 <form runat="server">
   <asp:dropdownlist id="myDropDownList" runat="server" />
   <asp:button id="myButton" runat="server" text="OK" Onclick="Click" />
   

<asp:Label id="myLabel" runat="server" text="" /> </form>

</html>

      </source>
   
  


For each statement for Hashtable (C#)

   <source lang="csharp">

<%@Page Language="c#" debug="true"  %> <script runat="server" Language="c#">

 void Page_Load(object source, EventArgs e)
 {
   Hashtable myHashtable = new Hashtable();
   myHashtable["UK"] = "United Kingdom";
   myHashtable["US"] = "United States";
   myHashtable["DE"] = "Germany";
   if (!(Page.IsPostBack))
   {
     foreach (DictionaryEntry Item in myHashtable)
     {
       ListItem newListItem = new ListItem();
       newListItem.Text = Item.Value.ToString();
       newListItem.Value = Item.Key.ToString();
       myDropDownList.Items.Add(newListItem);
     }
   }
 }
 void Click(object source, EventArgs e)
 {
   myLabel.Text = myDropDownList.SelectedItem.Value;
 }

</script> <html>

 <form runat="server">
   <asp:dropdownlist id="myDropDownList" runat="server" />
   <asp:button id="myButton" runat="server" text="OK" Onclick="Click" />
   

<asp:Label id="myLabel" runat="server" text="" /> </form>

</html>

      </source>
   
  


Hash table: Date key and string value (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <script runat="server">

void Page_Load(){

   Hashtable myHashtable = new Hashtable();
   myHashtable[Convert.ToDateTime("1/1/2005")] = "Event on January 1st 2005";
   string CountryName;
   CountryName = Convert.ToString(myHashtable[Convert.ToDateTime("1/1/2005")]);   
   lblOut.Text = CountryName;

}

</script> <html>

   <head>
   </head>
   <body>
       <form runat="server">
       <asp:Label runat="server" ID = "lblOut">Label</asp:Label>
       </form>
   </body>

</html>

      </source>
   
  


Hashtable: int key and string value (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <script runat="server">

void Page_Load(){

   Hashtable myHashtable = new Hashtable();
   myHashtable[1] = "Canada Post";
   string CountryName;
   CountryName = Convert.ToString(myHashtable[1]);   
   lblOut.Text = CountryName;

}

</script> <html>

   <head>
   </head>
   <body>
       <form runat="server">
       <asp:Label runat="server" ID = "lblOut">Label</asp:Label>
       </form>
   </body>

</html>

      </source>
   
  


Hash table Key is case sensitive (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <script runat="server">

void Page_Load(){

   Hashtable myHashtable = new Hashtable();
   myHashtable["UK"] = "United Kingdom";
   string CountryName;
   CountryName = Convert.ToString(myHashtable["uk"]);   
   lblOut.Text = CountryName;

}

</script> <html>

   <head>
   </head>
   <body>
       <form runat="server">
       <asp:Label runat="server" ID = "lblOut">Label</asp:Label>
       </form>
   </body>

</html>

      </source>
   
  


Hashtable: string key and string value (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <script runat="server">

void Page_Load(){

   Hashtable myHashtable = new Hashtable();
   myHashtable["UK"] = "United Kingdom";
   string CountryName;
   CountryName = Convert.ToString(myHashtable["UK"]);   
   lblOut.Text = CountryName;

}

</script> <html>

   <head>
   </head>
   <body>
       <form runat="server">
       <asp:Label runat="server" ID = "lblOut">Label</asp:Label>
       </form>
   </body>

</html>

      </source>