Share via


ShowItemCommands Event

Occurs before the commands related to an item in an ObjectList control are shown.

public event ObjectListShowCommandsEventHandler ShowItemCommands

Remarks

The event argument contains a reference to the item and to the collection of commands. An event handler can customize the collection according to the item. Applications can use this to provide functionality equivalent to item-specific shortcut menus. Changes made to the collection of commands during this event are not persisted.

This event is called after the ItemSelect event is called.

Example

The following example demonstrates how to trap the ShowItemCommands event to remove an item from the ObjectListCommandCollection object of the detail view of an ObjectList control. The item to remove is selected in the list view. By trapping this event, you can perform other tasks before showing the detail view. You can add, remove, or rearrange fields, depending on the item selected in the list view.

[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>

See Also

OnShowItemCommands Method | ObjectList Control