ASP.Net/Collections/SortedList

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

Sorted List Demo (C#)

   <source lang="csharp">

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

      void Page_Load()
      {
           SortedList stlShippers = new SortedList();
       
           stlShippers["cp"]="CP";
           stlShippers["fe"]="FE";
           stlShippers["us"]="US";
       
           lblOut.Text = "Your selection = " + stlShippers[txtCodeIn.Text];
      }

</script> <html>

   <head>
   </head>
   <body>
       <form runat="server">
       Please enter a shipper code ("cp" "fe" "us"): 
       <asp:TextBox runat="server" ID="txtCodeIn"></asp:TextBox>
<asp:Button runat="server" Text="Button"></asp:Button>
<asp:Label runat="server" ID = "lblOut">Label</asp:Label> </form> </body>

</html>

      </source>
   
  


SortedList Demo (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 Sortedlist()
   
   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>
   
  


Use SortedList (C#)

   <source lang="csharp">

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

 void Page_Load(object source, EventArgs e)
 {
   SortedList mySortedList = new SortedList();
   mySortedList["a"]="aa";
   mySortedList["b"]="bb";
   mySortedList["c"]="cc";
   mySortedList["d"]="dd";
   if (!(Page.IsPostBack))
   {
     foreach (DictionaryEntry Item in mySortedList)
     {
       ListItem newListItem = new ListItem();
       newListItem.Text = Item.Key.ToString();
       newListItem.Value = Item.Value.ToString();
       myDropDownList.Items.Add(newListItem);
     }
   }
 }
 void Click(object source, EventArgs e)
 {
   myLabel.Text = myDropDownList.SelectedItem.Value;
 }

</script> <html>

 <form runat="server">
   Pick a word from the list:
   <asp:dropdownlist id="myDropDownList" runat="server" />
   <asp:button id="myButton" runat="server" text="OK" Onclick="Click" />
   

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

</html>

      </source>