Latest web development tutorials

ASP.NET Hashtable

oggetto Hashtable contiene oggetti con le coppie chiave / valore espresso.


Esempi

Prova - Esempio

Tabella hash RadioButtonList 1

Tabella hash RadioButtonList 2

tabella hash DropDownList


creare Hashtable

oggetto Hashtable contiene oggetti con le coppie chiave / valore espresso. Chiave viene utilizzato come indice di ricerca valore della chiave può essere realizzato su una rapida ricerca.

Aggiungere elementi alla tabella hash attraverso il metodo add ().

Il codice seguente crea un oggetto Hashtable chiamato mycountries, e aggiungere i quattro elementi:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
end if
end sub
</script>


Data Binding

oggetto Hashtable 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):

<html>
<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:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
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

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
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 >>

Nota: non èpossibile scegliere di aggiungere alle voci Hashtable sono ordinati. Per il progetto o alfabetico ordine numerico, utilizzare l'oggetto SortedList.