Evento ShowItemCommands
Si verifica prima della visualizzazione dei comandi correlati a un elemento in un controllo ObjectList.
public event ObjectListShowCommandsEventHandler ShowItemCommands
Osservazioni
L'argomento dell'evento contiene un riferimento all'elemento e all'insieme dei comandi. Un gestore eventi è in grado di personalizzare l'insieme in base all'elemento. Questa caratteristica consente alle applicazioni di rendere risponibile la funzionalità equivalente ai menu di scelta rapida specifici dell'elemento. Durante tale evento non vengono mantenute le modifiche apportate all'insieme dei comandi.
Questo evento viene chiamato dopo l'evento ItemSelect.
Esempio
Nell'esempio riportato di seguito viene mostrato come intercettare l'evento ShowItemCommands per rimuovere un elemento dall'oggetto ObjectListCommandCollection della visualizzazione Dettagli relativa a un controllo ObjectList. L'elemento da rimuovere è selezionato nella visualizzazione elenco. Mediante l'intercettazione di questo evento è possibile eseguire altre attività prima di mostrare la visualizzazione Dettagli. È infatti possibileaggiungere, rimuovere o riordinare i campi, a seconda dell'elemento selezionato nella visualizzazione elenco.
[Visual Basic]
<SCRIPT language="vb" runat="server">
Dim item As System.Web.UI.MobileControls.ObjectListItem
Dim itemColl As System.Web.UI.MobileControls.ObjectListItemCollection
' Persist the array through subsequent page loads.
Dim arr As New ArrayList()
Class Task
Private _TaskName As String
Private _Editable As String
Private _Priority As Integer
Public Sub New(TaskName As String, Editable As String, Priority As Integer)
_TaskName = TaskName
_Editable = Editable
_Priority = Priority
End Sub
Public ReadOnly Property TaskName() As String
Get
Return _TaskName
End Get
End Property
Public ReadOnly Property Editable() As String
Get
Return _Editable
End Get
End Property
Public ReadOnly Property Priority() As Integer
Get
Return _Priority
End Get
End Property
End Class
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
' Create and fill of array.
arr.Add(New Task("Tomorrow's work", "yes", 1))
arr.Add(New Task("Today's work", "yes", 1))
arr.Add(New Task("Yesterday's work", "No", 1))
' Associate and bind array to ObjectList for each postback.
Session("MyArrayList") = arr
ObjectList1.DataSource = arr
ObjectList1.LabelField = "TaskName"
ObjectList1.DataBind()
End If
End Sub
Sub ShowTaskDetail(sender As Object, e As ObjectListSelectEventArgs)
' Check conditions, and add or remove commands in the detail view.
If e.ListItem("Editable").Equals("No") Then
ObjectList1.Commands.RemoveAt(0)
Else If (ObjectList1.Commands.Count < 1)
ObjectList1.Commands.Add(New ObjectListCommand("Delete", "Delete"))
End If
End Sub
Sub SelectCommand(sender As Object, e As ObjectListCommandEventArgs)
arr = CType(Session("MyArrayList"), ArrayList)
Dim i As Integer = ObjectList1.SelectedIndex
arr.RemoveAt(i)
Session("MyArrayList") = arr
'Re-Bind ObjectList to altered ArrayList.
ObjectList1.DataSource = arr
ObjectList1.LabelField = "TaskName"
ObjectList1.DataBind()
'Go back to the list view
ObjectList1.ViewMode = ObjectListViewMode.List
End Sub
</SCRIPT>
<mobile:Form runat="server" id="Form1">
<mobile:ObjectList id="ObjectList1" runat="server"
OnItemCommand="SelectCommand" OnShowItemCommands="ShowTaskDetail" >
<Command Name="Delete" Text="Delete" />
</mobile:ObjectList>
</mobile:Form>
<script runat=server>
System.Web.UI.MobileControls.ObjectListItem item;
System.Web.UI.MobileControls.ObjectListItemCollection itemColl;
// Persist the array through subsequent page loads.
ArrayList arr = new ArrayList();
class Task
{
private string _TaskName;
private string _Editable;
private int _Priority;
public Task(string TaskName, string Editable, int Priority)
{
_TaskName = TaskName;
_Editable = Editable;
_Priority = Priority;
}
public string TaskName { get { return _TaskName; } }
public string Editable { get { return _Editable; } }
public int Priority { get { return _Priority; } }
}
public void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
// Create and fill of array.
arr.Add (new Task ("Tomorrow's work", "yes", 1));
arr.Add (new Task ("Today's work", "yes", 1));
arr.Add (new Task ("Yesterday's work", "No", 1));
// Associate and bind array to ObjectList for each postback.
Session["MyArrayList"] = arr;
ObjectList1.DataSource = arr;
ObjectList1.LabelField = "TaskName";
ObjectList1.DataBind();
}
}
void SelectCommand(Object sender, ObjectListCommandEventArgs e)
{
arr = (ArrayList)Session["MyArrayList"];
// Remove selected item from the ObjectList object using the array
int i = ObjectList1.SelectedIndex;
arr.RemoveAt(i);
Session["MyArrayList"] = arr;
// Re-Bind ObjectList to altered ArrayList.
ObjectList1.DataSource = arr;
ObjectList1.LabelField = "TaskName";
ObjectList1.DataBind();
ObjectList1.ViewMode = ObjectListViewMode.List;
}
void ShowTaskDetail(Object sender, ObjectListSelectEventArgs e)
{
// Check conditions, and add or remove commands in the detail view.
if(e.ListItem["Editable"].Equals("No"))
{
ObjectList1.Commands.RemoveAt(0);
}
Else If (ObjectList1.Commands.Count < 1)
{
ObjectList1.Commands.Add(New ObjectListCommand("Delete", "Delete"));
}
}
</script>
<mobile:Form runat=server id="Form1" >
<mobile:ObjectList runat="server" id="ObjectList1"
OnItemCommand="SelectCommand"
OnShowItemCommands="ShowTaskDetail" >
<Command Name="Delete" Text="Delete" />
</mobile:ObjectList>
<mobile:Label runat=server id="Label1" />
<mobile:Label runat=server id="Label2" />
</mobile:Form>
Vedere anche
Metodo OnShowItemCommands | Controllo ObjectList