Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Aggiornamento: novembre 2007
Benché i controlli SelectionList e List siano simili, le relative funzionalità si differenziano in modo significativo in fase di progettazione e in fase di esecuzione. Entrambe le classi gestiscono un insieme di elementi dell'elenco. Mentre tuttavia il controllo List deriva dalla classe PagedControl e in definitiva dalla classe MobileControl, il controllo SelectionList deriva direttamente dalla classe MobileControl e non dispone di proprietà di gestione dell'impaginazione, ad esempio la proprietà ItemWeight.
La differenza principale tra le classi è costituita dal fatto che la classe SelectionList supporta la selezione di uno o più elementi. Nella proprietà SelectType è contenuto il valore enumerato ListSelectType, che determina se la classe SelectionList è in modalità di selezione singola o multipla.
La classe List consente di selezionare solo singoli elementi in un elenco. La classe SelectionList invece consente di specificare diversi tipi di elenco, tra cui CheckBox, DropDown, ListBox, MultiSelectListBox e Radio.
Funzionalità di SelectionList
Il controllo SelectionList è in modalità di selezione singola se la proprietà SelectType viene impostata su uno dei seguenti valori:
Gestione della selezione
Per recuperare l'elemento attualmente selezionato in un controllo SelectionList mentre è attiva la modalità di selezione singola, utilizzare le proprietà SelectedIndex e Selection.
I valori enumerati CheckBox e MultiSelectListBox indicano la modalità di selezione multipla. Per recuperare la selezione, eseguire una query sulla proprietà Selected di ciascun elemento.
Nell'esempio riportato di seguito viene illustrato come recuperare da un elenco a selezione multipla i valori selezionati.
<%@ Page Language="VB"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script >
private class Person
' Private Fields
Private _Name, _Nickname As String
' Constructor
Public Sub New(ByVal name As String, _
ByVal nickname As String)
Me._Name = name
Me._Nickname = nickname
End Sub
' Public Properties
Public ReadOnly Property Name()
Get
Return _Name
End Get
End Property
Public ReadOnly Property Nickname()
Get
Return _Nickname
End Get
End Property
End Class
' An ArrayList for the Person objects
Dim presidents = New ArrayList()
Private Sub Page_Load(ByVal sender As Object, _
ByVal e As EventArgs)
' Fill the presidents ArrayList
presidents.Add( _
New Person("George Washington", "George"))
presidents.Add( _
New Person("Abraham Lincoln", "Abe"))
presidents.Add( _
New Person("Theodore Roosevelt", "Teddy"))
If Not IsPostBack Then
' Bind the array to the list.
SelectionList1.DataSource = presidents
' Specify the field to display
SelectionList1.DataValueField = "Name"
SelectionList1.DataBind()
End If
End Sub
Protected Sub Command1_Click( _
ByVal sender As Object, ByVal e As EventArgs)
Dim retval As String = String.Empty
Dim per As Person
If Not SelectionList1.IsMultiSelect Then
retval = "Value: "
' Get the selected item
per = CType(presidents(SelectionList1.SelectedIndex), Person)
' Get the text of the item
If Not IsNothing(per) Then
retval &= per.Name & "(" & per.Nickname & ")"
End If
ElseIf SelectionList1.IsMultiSelect Then
retval = "Values: "
' Gather the text from list items
Dim li As MobileListItem
Dim i As Integer = 0
For i = 0 To SelectionList1.Items.Count - 1
li = SelectionList1.Items(i)
' Gather text only from selected items
If li.Selected Then
per = CType(presidents(li.Index), Person)
retval &= per.Name & "(" & per.Nickname & "), "
End If
Next
End If
' Clean ending comma, if any
If retval.IndexOf(", ") > -1 Then
retval = retval.Substring(0, retval.Length - 2)
End If
' Put return value into the Label
Label1.Text = retval
' Activate Form2
Me.ActiveForm = Form2
End Sub
Protected Sub Command2_Click( _
ByVal sender As Object, ByVal e As EventArgs)
' Activate Form1
Me.ActiveForm = Form1
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:form id="Form1" >
Select several items in the list:<br />
<mobile:SelectionList ID="SelectionList1"
Runat="server" SelectType="Checkbox">
</mobile:SelectionList>
<mobile:Command ID="Command1" Runat="server"
OnClick="Command1_Click">
Record Choices
</mobile:Command>
</mobile:form>
<mobile:Form ID="Form2" Runat="server">
<mobile:Label ID="Label1" />
<mobile:Command ID="Command2" Runat="server"
OnClick="Command2_Click">Return
</mobile:Command>
</mobile:Form>
</body>
</html>
<%@ Page Language="C#"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script >
private class Person
{
// Private Fields
private String _Name, _Nickname;
// Constructor
public Person(string name, string nickname)
{
this._Name = name;
this._Nickname = nickname;
}
// Public Properties
public String Name { get { return _Name; } }
public String Nickname { get { return _Nickname; } }
}
// An ArrayList for the Person objects
ArrayList presidents = new ArrayList();
private void Page_Load(object sender, System.EventArgs e)
{
// Fill the Person object
presidents.Add(
new Person("George Washington", "George"));
presidents.Add(
new Person("Abraham Lincoln", "Abe"));
presidents.Add(
new Person("Theodore Roosevelt", "Teddy"));
if (!IsPostBack)
{
// Bind the array to the list.
SelectionList1.DataSource = presidents;
// Specify the field to display
SelectionList1.DataValueField = "Nickname";
SelectionList1.DataBind();
}
}
protected void Command1_Click(object sender, EventArgs e)
{
string retval = String.Empty;
Person per;
if (!SelectionList1.IsMultiSelect)
{
retval = "Value: ";
// Get the selected item
per = (Person)presidents[SelectionList1.SelectedIndex];
// Get the name and nickname of the person
if (per != null)
retval += per.Name + " (" + per.Nickname + ")";
}
else if (SelectionList1.IsMultiSelect)
{
retval = "Values: ";
// Gather the text from list items
foreach (MobileListItem li in SelectionList1.Items)
{
// Gather text only from selected items
if (li.Selected)
{
per = (Person)presidents[li.Index];
retval += per.Name + " (" + per.Nickname + "), ";
}
}
}
// Clean ending comma, if any
if (retval.IndexOf(", ") > -1)
retval = retval.Substring(0, retval.Length - 2);
// Put return value into the Label
Label1.Text = retval;
// Activate Form2
this.ActiveForm = Form2;
}
protected void Command2_Click(object sender, EventArgs e)
{
// Activate Form1
this.ActiveForm = Form1;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:form id="Form1" >
Select several items in the list:<br />
<mobile:SelectionList ID="SelectionList1"
Runat="server" SelectType="Checkbox">
</mobile:SelectionList>
<mobile:Command ID="Command1" Runat="server"
OnClick="Command1_Click">
Record Choices
</mobile:Command>
</mobile:form>
<mobile:Form ID="Form2" Runat="server">
<mobile:Label ID="Label1" />
<mobile:Command ID="Command2" Runat="server"
OnClick="Command2_Click">Return
</mobile:Command>
</mobile:Form>
</body>
</html>
Aggiunta di elementi a un controllo List
In un controllo List è contenuto un insieme di elementi della classe MobileListItem. Esistono diversi modi per aggiungere elementi a un controllo List:
Creare elementi <Item> in un elenco. Ciascun elemento <Item> diventa una classe MobileListItem nell'elenco e le proprietà vengono impostate da attributi dell'elemento <Item> .
Aggiungere elementi all'elenco a livello di codice utilizzando l'insieme Items del controllo List. È possibile creare un oggetto MobileListItem e aggiungerlo all'insieme prima di eseguire il rendering.
Associare il controllo List a dati, in modo particolare a oggetti che implementano l'interfaccia IEnumerable o IListSource, ad esempio oggetti ArrayList o DataSet.
Vedere anche
Concetti
Accesso ai dati mediante i controlli elenco
Riferimenti
Altre risorse
Accesso ai dati tramite ASP.NET