Condividi tramite


Classe ObjectListCommandCollection

Rappresenta un insieme di oggetti ObjectListCommand.

public class System.Web.UI.MobileControls.ObjectListCommandCollection : 
   System.Web.UI.MobileControls.ArrayListCollectionBase,
   System.Web.UI.IStateManager

Osservazioni

L'oggetto di classe rende disponibile il contenitore per i comandi di un controllo ObjectList. L'accesso alla proprietà Commands di un controllo ObjectList consente di recuperare un oggetto ObjectListCommandCollection. Un'applicazione può aggiungere o rimuovere comandi a livello di codice.

La classe ObjectListCommandCollection implementa l'interfaccia System.Collections.ICollection tramite la classe System.Web.UI.MobileControls.ArrayListCollectionBase.

Esempio

Nell'esempio riportato di seguito, un matrice di attività persiste in più caricamenti di pagina e viene associata a un elenco di oggetti da visualizzare.

<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" 
   Language="c#" debug="true" %>

<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 the 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 the array to ObjectList for each postback.
      Session["MyArrayList"] = arr;
      ObjectList1.DetailsCommandText = "MoreDetails";
   }
   arr = (ArrayList)Session["MyArrayList"];
   ObjectList1.DataSource = arr;
   ObjectList1.LabelField = "TaskName";
   ObjectList1.DataBind();
}

void SelectCommand(Object sender, ObjectListCommandEventArgs e)
{
   String myCommand;
   myCommand = e.CommandName;
   switch (myCommand)
   {
      // Remove selected item from the ObjectList using the array.
      case "Delete":
         int i = ObjectList1.SelectedIndex;
         arr.RemoveAt(i);
         Label2.Text = ObjectList1.Selection["TaskName"] + " deleted";
         Session["MyArrayList"] = arr;
         break;

      // Edit selected item (code below is non-functional).
      case "Edit":
         /* 
          { insert code to edit the item }
         */
         Label2.Text = ObjectList1.Selection["TaskName"] + " edit";
         break;
   }
}

void ShowTaskDetail(Object sender, ObjectListSelectEventArgs e)
{
   // Check for aguments that start with "ShowMore" so you can view 
   // the details of an item.
   if(e.SelectMore)
   {
      //Add or remove commands in the details view.
      if(e.ListItem["Editable"].ToString()=="No")
      {
         ObjectList1.Commands.RemoveAt(1);
      }
      if(e.ListItem["TaskName"].ToString()=="Tomorrow's work")
      {
         ObjectList1.Commands.RemoveAt(0);
      }
   }
   else
   {
      // If an argument for a selected item does not exist,
      // then do not show the details view.
      e.UseDefaultHandling = false;
   }
}
</script>

<mobile:Form runat=server id="Form1" >
   <mobile:ObjectList runat="server" id="ObjectList1" 
      OnItemCommand="SelectCommand" 
      OnItemSelect="ShowTaskDetail" >
      <Command Name="Delete" Text="Delete" />
      <Command Name="Edit" Text="Edit" />
   </mobile:ObjectList>
   <mobile:Label runat=server id="Label1" />
   <mobile:Label runat=server id="Label2" />
</mobile:Form>

Requisiti

Spazio dei nomi: System.Web.UI.MobileControls

Assembly: System.Web.Mobile