Share via


ItemDataBind Event (ObjectList)

Occurs when an item in a ObjectList control is bound to data.

public event ObjectListDataBindEventHandler ItemDataBind

Remarks

When an item in an ObjectList control is created and data-bound, this event handler sets the properties of the list item from arbitrary expressions.

An item in an ObjectList control is of type ObjectListItem.

Example

The following example demonstrates how to trap the ItemDataBind event. You can use either the ListItem or DataItem property of the ObjectListDataBindEventArgs collection to determine further action. For example, you can add code to order the book for the customer if the value of the DataItem property is Yes.

[Visual Basic]

<SCRIPT language="vb" runat="server">

Shared i, j As Integer

Dim item As System.Web.UI.MobileControls.ObjectListItem
Dim itemColl As System.Web.UI.MobileControls.ObjectListItemCollection

Class Book
   Private _BookName, _Author, _InStock As String
   
   
   Public Sub New(BookName As String, Author As String, InStock As String)
      _BookName = BookName
      _Author = Author
      _InStock = InStock
   End Sub
   
   
   Public ReadOnly Property BookName() As String
      Get
         Return _BookName
      End Get
   End Property
   
   Public ReadOnly Property Author() As String
      Get
         Return _Author
      End Get
   End Property
   
   Public ReadOnly Property InStock() As String
      Get
         Return _InStock
      End Get
   End Property
End Class 


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


  If Not IsPostBack Then
   ' Create and fill array.
   Dim arr As New ArrayList()
   arr.Add(New Book("Catch-22", "Joseph Heller", "Yes"))
   arr.Add(New Book("Mother Night", "Vonnegut", "No"))
   arr.Add(New Book("Lord Jim", "Conrad", "Yes"))
   arr.Add(New Book("Huckleberry Finn", "Twain", "No"))
   
   ' Associate and bind the array to the ObjectList control.
   ObjectList1.DataSource = arr
   ObjectList1.DataBind()
   
   ' Override autogeneration.
   ObjectList1.TableFields = "BookName;InStock"
   ObjectList1.LabelField = "BookName"
Else
   i = 0
   j = 0
End If

End Sub


Sub confirmBound(sender As Object, e As ObjectListDataBindEventArgs)
   
   Select Case CType(e.DataItem, Book).InStock
      Case "Yes"
         i += 1
      Case "No"
         j += 1
   End Select
   
   Label1.Text = "You have " + i.ToString() + " Books InStock in stock and " + j.ToString() + " Books not in stock"
End Sub

</script>

<mobile:Form runat=server id="Form1" >
   <mobile:ObjectList runat="server" id="ObjectList1" 
           OnItemDataBind="confirmBound" AutoGenerateFields=true>
   </mobile:ObjectList>
   <mobile:Label runat=server id="Label1" />
</mobile:Form>
<script language="c#" runat="server">
// Persist across multiple postbacks.
static int i,j;

System.Web.UI.MobileControls.ObjectListItem item;
System.Web.UI.MobileControls.ObjectListItemCollection itemColl;

class Book
{
   private String _BookName;
   private String _Author;
   private String _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 ("Catch-22", "Joseph Heller", "Yes"));
     arr.Add (new Book ("Mother Night", "Vonnegut", "No"));
     arr.Add (new Book ("Lord Jim", "Conrad", "Yes"));
     arr.Add (new Book ("Huckleberry Finn", "Twain", "No"));

     // Associate and bind the array to the ObjectList control.
     ObjectList1.DataSource = arr;
     ObjectList1.DataBind ();

     // Override autogeneration.
     ObjectList1.TableFields = "BookName;InStock";
     ObjectList1.LabelField = "BookName";
   }
   else
   {
     i=j=0;
   }
}

void confirmBound(object sender, ObjectListDataBindEventArgs e)
{

switch (((Book)(e.DataItem)).InStock)
   {
      case "Yes":
         i++;
         break;
      case "No":
         j++;
         break;
      Default:
         break;
   }

   Label1.Text = "You have " + i.ToString() + " Books in stock and " 
                             + j.ToString() + " Books not in stock";
}
</script>

<mobile:Form runat=server id="Form1" >
   <mobile:ObjectList runat="server" id="ObjectList1" 
           OnItemDataBind="confirmBound" AutoGenerateFields=true>
   </mobile:ObjectList>
   <mobile:Label runat=server id="Label1" />
</mobile:Form>

See Also

ObjectListDataBindEventArgs Class | ItemDataBind Event (List) | List Class | ListDataBindEventArgs Class (SelectionList) | SelectionList Class | ListDataBindEventArgs Class | OnItemDataBind Method | ObjectList Class