ASP.NET SortedList
oggetti SortedList combina le proprietà di un oggetto e l'oggetto ArrayList Hashtable.
Prova - Esempio
oggetti SortedList
oggetto SortedList contiene oggetti con la chiave / coppie di valori espressi. SortedList oggetti ordinare automaticamente le voci in ordine alfabetico o numerico.
Con il metodo add () per aggiungere elementi alla SortedList. Con il metodo TrimToSize () per regolare la SortedList finale dimensioni.
Il codice seguente crea un oggetto SortedList chiamato mycountries, e aggiungere i quattro elementi:
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
end if
end sub
</script>
Data Binding
SortedList oggetto in grado di generare automaticamente il testo e valori per i seguenti controlli:
- asp: RadioButtonList
- asp: CheckBoxList
- asp: DropDownList
- asp: Listbox
Per associare i dati a un controllo RadioButtonList, prima creare un controllo RadioButtonList a una pagina aspx (senza asp: ListItem elementi):
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
</body>
</html>
Quindi aggiungere lo script per creare l'elenco, ed i valori vincolanti nella lista per il controllo RadioButtonList:
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
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>
Poi aggiungiamo una subroutine, quando un utente fa clic su un elemento nel controllo RadioButtonList quando viene eseguito il sottoprogramma. Quando un pulsante viene premuto, l'etichetta apparirà nella riga di testo:
Esempi
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
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 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>
La dimostrazione >>