Share via


ObjectListFieldCollection Class

Represents a collection of ObjectListField objects.

public class System.Web.UI.MobileControls.ObjectListFieldCollection : 
   System.Web.UI.MobileControls.ArrayListCollectionBase,
   System.Web.UI.MobileControls.IObjectListFieldCollection,
   System.Web.UI.IStateManager

Remarks

This class object provides the container for the fields of an ObjectList control. Accessing the Fields property of an ObjectList control retrieves an ObjectListFieldCollection object.

The properties and methods of the ObjectListFieldCollection class are a superset of the IObjectListFieldCollection interface. All properties and methods of that interface, including those that it inherits from the ASP.NET System.Collections.ICollection interface, are applicable here. This class provides additional properties and methods to modify the collection. These properties and methods provide standard collection management capability, such as adding, removing, and clearing the collection.

If you change the collection by calling any of the following methods during Page_Init, the view state for the Fields, Items, and ViewMode properties of the object list is ignored:

Add, AddAt, Remove, or RemoveAt

Example

During the initial page load, this example creates instances of each ObjectListField object and sets its DataField and Title properties. The example then explicitly adds the ObjectListField items to the ObjectListFieldCollection object.

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

<script Language="c#" runat="server">

class Book
{
   private String _BookName, _Author, _InStock;
   
   public Book(String BookName, String Author, String InStock) 
   { 
      _BookName = BookName; 
      _Author = Author;
      _InStock = InStock;
   }

   public String BookName { get { return _BookName; } }
   public String Author { get { return _Author; } }
   public String InStock { get { return _InStock; } }
}

public void Page_Load(Object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      // Create and fill array.
      ArrayList arr = new ArrayList();
      arr.Add (new Book ("Sushi, Anyone?", "O'Leary", "Yes"));
      arr.Add (new Book ("Secrets of Silicon Valley", "Dull", "No"));
      arr.Add (new Book ("Net Etiquette", "Locksley", "Yes"));
      arr.Add (new Book ("The Gourmet Microwave", "Ringer", "No"));

      // Associate array to ObjectList.
      ObjectList1.DataSource = arr;

      // Use the Title attribute to specify the field to be used 
      // as a title.
      ObjectList1.LabelField = "BookName";
      ObjectList1.AutoGenerateFields=false ;

      // Add the ObjectListFields to the ObjectListFieldCollection
      // of ObjectList1.
      ObjectListField ListField1 = new ObjectListField();
      ListField1.DataField = "BookName";
      ListField1.Title = "BookName";
      ObjectList1.Fields.Add(ListField1);

      ListField1 = new ObjectListField();
      ListField1.DataField = "Author";
      ListField1.Title = "Author";
      ObjectList1.Fields.Add(ListField1);

      ListField1 = new ObjectListField();
      ListField1.DataField = "InStock";
      ListField1.Title = "InStock";
      ObjectList1.Fields.Add(ListField1);

      // Bind to the ObjectList.
      ObjectList1.DataBind();

      // Store the datasource in a session variable so that
      // the datasource can persist across multiple postbacks.
      Session["MyArray"] = arr;
   }

   // Access all fields by using a foreach loop.
   foreach (ObjectListField f in ObjectList1.Fields)
   {
     TxtVw.Text += f.Title + " field is bound to DataItem " + 
       f.DataField.ToString() + "</br>";     
   }
}

void CommandClick(Object sender, ObjectListCommandEventArgs e)
{
   ObjectList1.Fields.RemoveAt(1);

   // Associate the array to an object list.
   ObjectList1.DataSource = (ArrayList) Session["MyArray"];

   // Must bind data again.
   ObjectList1.DataBind();
}
</script>

<mobile:Form runat="server" id="Form1" >
   <mobile:ObjectList runat="server" id="ObjectList1" 
      OnItemCommand="CommandClick" >
      <command Name="RemoveField" Text="Remove Second Field" />
   </mobile:ObjectList>
   <mobile:Label runat="server" id="Label1" />
   <mobile:Label runat="server" id="Label2" />
   <mobile:TextView runat=server ID="TxtVw" />
</mobile:Form>

Requirements

Namespace: System.Web.UI.MobileControls

Assembly: System.Web.Mobile

See Also

ObjectListField Class