Share via


ItemDataBind Event (List)

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

public event ListDataBindEventHandler ItemDataBind

Remarks

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

An item in a List control is of type MobileListItem.

This event is raised as each data source item is bound. This is the best time to filter or group items in the MobileListitemCollection collection, by using the Text or Value.

Example

The following example demonstrates how to trap the ItemDataBind event. The DataValueField property specifies what action is performed.

   <mobile:Label runat="server" id="Label1" ForeColor=green 
                 Font-Italic=true />
   <mobile:List runat=server id="List1" OnItemDataBind="TaskSummary" 
                OnItemCommand = "TaskStatus"/>
   <mobile:Label runat="server" id="Label2" Font-Italic=true 
                 ForeColor=Red />
   <mobile:Label runat="server" id="Label3" Font-Italic=true 
                 ForeColor=Red />
</mobile:Form>
   

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

<script runat=server>

// Persist across multiple postbacks.
static int i,j,k;

class Task
{
   private String _TaskName;
   private String _Status;

   public Task(String TaskName, String Status) 
   { 
      _TaskName = TaskName; 
      _Status = Status;
   }

   public String TaskName { get { return _TaskName; } }
   public String Status { get { return _Status; } }
}

public void Page_Load(Object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      // Initialize static variables to zero.
      i = j = k = 0;

      // Initial settings for tasks.
      List1.DataValueField="Status";
      List1.DataTextField="TaskName";

      // Create an array and add the tasks to it.
      ArrayList arr = new ArrayList();
      arr.Add (new Task ("Verify transactions", "Done"));
      arr.Add (new Task ("Check balance sheet", "Scheduled"));
      arr.Add (new Task ("Send report", "Pending"));

      // Associate and bind the List to the array.
      List1.DataSource = arr;
      List1.DataBind ();

      if (List1.HasItemCommandHandler)
      {
         Label1.Text = "List has ItemCommand event handler";
      }
      else
      {
         Label1.Text = "List does not have ItemCommand event handler";
      }
   }
}

void TaskSummary(Object sender, ListDataBindEventArgs e)
{ 
   switch (e.ListItem.Value)
   {
      case "Done":
         i++;
         break;
      case "Scheduled":
         j++;
         break;
      case "Pending":
         k++;
         break;
      Default:
         break;
   }

   Label2.Text = "You have " + i + " tasks done, " 
                             + j + " tasks scheduled, and " 
                             + k + " tasks Pending.";
}

void TaskStatus(Object sender, ListCommandEventArgs e)
{
   Label3.Text = e.ListItem.Text +" is " + e.ListItem.Value;
}


</script>

<mobile:Form runat=server id="frm1" >
   <mobile:Label runat="server" id="Label1" ForeColor=green 
                 Font-Italic=true />
   <mobile:List runat=server id="List1" OnItemDataBind="TaskSummary" 
                OnItemCommand = "TaskStatus"/>
   <mobile:Label runat="server" id="Label2" Font-Italic=true 
                 ForeColor=Red />
   <mobile:Label runat="server" id="Label3" Font-Italic=true 
                 ForeColor=Red />
</mobile:Form>

See Also

ListDataBindEventArgs Class (SelectionList) | ObjectList Class | ObjectListDataBindEventArgs Class | SelectionList Class | OnItemDataBind Method

Applies to: List Class