Share via


ObjectListField Class

Represents a field of an ObjectList ASP.NET mobile control.

public sealed class System.Web.UI.MobileControls.ObjectListField : 
   System.Object, System.Web.UI.IStateManager

Remarks

A field is of type ObjectListField. When you want to display multiple fields, you can specify multiple fields for each item in an object list. To create a field within the declaration of an ObjectList control, you declare an ObjectListField as a <Field> element. Each field is associated with a property name. When a list item is bound to a data object, each field is bound to the corresponding property of the object.

The ObjectList control provides three ways to define a field:

  • Declaratively, by using <Field> child elements within the object list
  • Programmatically, by constructing ObjectListField objects and adding them to the Fields member collection of the control
  • Automatically, by setting the AutoGenerateFields property to true

Example

The following code example programmatically adds an item to the ObjectListFieldCollection collection of an object list during the initial page load. In this way, you can explicitly control the addition of fields to an object list.

<%@ 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 the array with an object list.
      ObjectList1.DataSource = arr;

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

      // Add an ObjectListField object
      // to the ObjectListFieldCollection object.
      // collection 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 object.
      ObjectList1.DataBind();

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

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

   // Associate the array to the ObjectList object.
   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:Form>

Requirements

Namespace: System.Web.UI.MobileControls

Assembly: System.Web.Mobile

See Also

ObjectListFieldCollection Class