ASP.NET - SortedList-object
- Previous page WebForms Hashtable
- Next page WebForms XML File
De SortedList heeft de eigenschappen van zowel een ArrayList als een Hashtable-object.
SortedList-object
De SortedList-object bevat items weergegeven als sleutel/waarde-paren. De SortedList kan items automatisch sorteren op letterschijving of numerieke volgorde.
Items worden toegevoegd aan de SortedList via de Add() methode. De SortedList kan worden aangepast tot de uiteindelijke grootte met de TrimToSize() methode.
Deze code maakt een SortedList genaamd mycountries en voegt vier elementen toe:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add("C","China") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountries.Add("I","Italy") end if end sub </script>
Gegevenskoppeling
De SortedList-object kan automatisch tekst en waarden genereren voor de onderstaande knoppen:
- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
Om gegevens te koppelen aan de RadioButtonList-knop, maak je eerst een RadioButtonList-knop aan in het aspx-bestand (zonder enige asp:ListItem-elementen):
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html>
Vervolgens voeg je het script toe om de lijst te bouwen:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add("C","China") 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 subroutine that will be executed when the user clicks on an item in the RadioButtonList control. When the radio button is clicked, the text will appear in the label:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add("C","China") 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 favorite country 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>
- Previous page WebForms Hashtable
- Next page WebForms XML File