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
Nei passaggi che seguono viene descritto come un controllo List mobile crei un'istanza della classe MobileListItem da un elemento dell'insieme:
Il controllo verifica se sono state definite le proprietà DataTextField o DataValueField. In caso affermativo, vengono utilizzati questi nomi di campo per individuare le proprietà di un elemento e impostare le proprietà Text e Value dell'istanza della classe MobileListItem.
Se non sono definite le proprietà DataTextField né DataValueField, il controllo imposta le proprietà Text e Value dell'istanza della classe MobileListItem sulla rappresentazione di stringa dell'elemento utilizzando il metodo ToString.
Se definito, viene chiamato il gestore eventi ItemDataBind. È possibile utilizzare questo gestore per impostare le proprietà dell'istanza della classe MobileListItem.
Quando viene fornito un rendering predefinito, il controllo List rappresenta un'istanza della classe MobileListItem in base alla relativa proprietà Text. Nel tipo di rendering basato sui modelli il modello consente di eseguire il rendering di una proprietà desiderata dell'istanza della classe MobileListItem o dell'oggetto associato a dati correlato.
Se la proprietà ItemsAsLinks è impostata, il controllo List esegue il rendering degli elementi sotto forma di collegamenti ipertestuali. Il valore della proprietà Text diventa il testo del collegamento, mentre quello della proprietà Value diventa l'URL di destinazione.
Gestione della selezione
Se l'elenco è complesso, ad esempio una matrice di oggetti, non è possibile accedere direttamente ai membri dell'elemento selezionato tramite la selezione. Tuttavia, se l'applicazione viene progettata correttamente, è possibile accedere all'oggetto associato. Se si crea un gruppo di oggetti da inserire nell'elenco, è possibile rendere il gruppo una matrice globale e quindi gestire il valore restituito come un indice nella matrice, come illustrato nell'esempio di codice riportato di seguito.
<%@ Page Language="VB"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script >
Private customers(3) As Person
Private Class Person
Private _Name, _Nickname, _Initials As String
Public Sub New(ByVal name As String, _
ByVal nickname As String, ByVal initials As String)
Me._Name = name
Me._Nickname = nickname
Me._Initials = initials
End Sub
Public ReadOnly Property Name() As String
Get
Return _Name
End Get
End Property
Public ReadOnly Property Nickname() As String
Get
Return _Nickname
End Get
End Property
Public ReadOnly Property Initials() As String
Get
Return _Initials
End Get
End Property
End Class
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
ReDim customers(2)
customers(0) = New Person("George Washington", "George", "GW")
customers(1) = New Person("Abraham Lincoln", "Abe", "AL")
customers(2) = New Person("Theodore Roosevelt", "Teddy", "TR")
If (Not IsPostBack) Then
' Bind the array to the list.
List1.DataSource = customers
List1.DataTextField = "Name"
List1.DataBind()
End If
End Sub
Protected Sub List1_ItemCommand(ByVal sender As Object, _
ByVal e As ListCommandEventArgs)
Dim selectedPerson As Person = customers(e.ListItem.Index)
Label1.Text = String.Format("{0} (AKA {1}), initials {2}", _
selectedPerson.Name, selectedPerson.Nickname, _
selectedPerson.Initials)
ActiveForm = Form2
End Sub
Protected Sub Command1_Click(ByVal sender As Object, _
ByVal e As EventArgs)
Me.ActiveForm = Me.Form1
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:form id="Form1" >
<mobile:List ID="List1" Runat="server" OnItemCommand="List1_ItemCommand">
</mobile:List>
</mobile:form>
<mobile:Form ID="Form2" Runat="server">
<mobile:Label ID="Label1" >Label</mobile:Label>
<mobile:Command ID="Command1" Runat="server" OnClick="Command1_Click">Return to Form1</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 Person[] customers = new Person[3];
private class Person
{
private String _Name, _Nickname, _Initials;
public Person(String name, String nickname, String initials)
{
this._Name = name;
this._Nickname = nickname;
this._Initials = initials;
}
public String Name { get { return _Name; } }
public String Nickname { get { return _Nickname; } }
public String Initials { get { return _Initials; } }
}
private void Page_Load(object sender, System.EventArgs e)
{
customers[0] = new Person("George Washington", "George", "GW");
customers[1] = new Person("Abraham Lincoln", "Abe", "AL");
customers[2] = new Person("Theodore Roosevelt", "Teddy", "TR");
if(!IsPostBack)
{
// Bind the array to the list.
List1.DataSource = customers;
List1.DataTextField = "Name";
List1.DataBind();
}
}
private void List1_ItemCommand(object sender,
ListCommandEventArgs e)
{
Person selectedPerson = customers[e.ListItem.Index];
Label1.Text = String.Format("{0} (AKA {1}), initials {2}",
selectedPerson.Name, selectedPerson.Nickname,
selectedPerson.Initials);
ActiveForm = Form2;
}
protected void Command1_Click(object sender, EventArgs e)
{
this.ActiveForm = this.Form1;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:form id="Form1" >
<mobile:List ID="List1" Runat="server" OnItemCommand="List1_ItemCommand">
</mobile:List>
</mobile:form>
<mobile:Form ID="Form2" Runat="server">
<mobile:Label ID="Label1" >Label</mobile:Label>
<mobile:Command ID="Command1" Runat="server" OnClick="Command1_Click">Return to Form1</mobile:Command>
</mobile:Form>
</body>
</html>