Share via


ItemDataBind Event (SelectionList)

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

public event ListDataBindEventHandler ItemDataBind

Remarks

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

An item in a SelectionList 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 of the MobileListitemCollection collection, by using the Text or Value property.

Example

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

[Visual Basic]

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

 ' Persist across multiple postbacks.
Shared i, j, k As Integer

Class Task
   Private _TaskName As String
   Private _Status As [String]
   
   
   Public Sub New(TaskName As [String], Status As [String])
      _TaskName = TaskName
      _Status = Status
   End Sub 
   
   
   Public ReadOnly Property TaskName() As [String]
      Get
         Return _TaskName
      End Get
   End Property
   
   Public ReadOnly Property Status() As [String]
      Get
         Return _Status
      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
   ' Initialize static variables to zero.
   i = 0
   j = 0
   k = 0

' Initial settings for tasks.
   SelectionList1.DataValueField = "Status"
   SelectionList1.DataTextField = "TaskName"
   
   ' Create an array and add the tasks to it.
   Dim arr As 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 SelectionList object
   ' to the array.
   SelectionList1.DataSource = arr
   SelectionList1.DataBind()
   Label2.Text = "Check boxes are checked for tasks having Done" + " Status"
End If
End Sub

Sub TaskSummary(sender As [Object], e As ListDataBindEventArgs)
   Select Case e.ListItem.Value
      Case "Done"
         i += 1
         'Check the check box for status.
         e.ListItem.Selected = True
      Case "Scheduled"
         j += 1
      Case "Pending"
         k += 1
   End Select
   
   
   Label1.Text = "You have " + i.ToString() + " tasks done, " + j.ToString() + " tasks scheduled, and " + k.ToString() + " tasks pending."
End Sub 'TaskSummary


    
</Script>
<body Xmlns:mobile="https://schemas.microsoft.com/Mobile/WebForm">
   <mobile:Form id="Form1" runat="server">
   <mobile:Label runat="server" id="Label1" ForeColor=green 
                 Font-Italic=true />
   <mobile:SelectionList runat=server id="SelectionList1" 
      OnItemDataBind="TaskSummary" SelectType="CheckBox"/>
   <mobile:Label runat="server" id="Label2" Font-Italic=true 
      ForeColor=Red />
</mobile:Form>
<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.
      SelectionList1.DataValueField="Status";
      SelectionList1.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 SelectionList object
      // to the array.
      SelectionList1.DataSource = arr;
      SelectionList1.DataBind ();
      Label2.Text = "Check boxes are checked for tasks having Done" +
                      " Status";
   }
}

void TaskSummary(Object sender, ListDataBindEventArgs e)
{ 
   switch (e.ListItem.Value)
   {
      case "Done":
         i++;
         //Check the check box for status.
         e.ListItem.Selected = true;
         break;
      case "Scheduled":
         j++;
         break;
      case "Pending":
         k++;
         break;
      Default:
         break;
   }

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


</script>

<mobile:Form runat=server id="frm1" >
   <mobile:Label runat="server" id="Label1" ForeColor=green 
                 Font-Italic=true />
   <mobile:SelectionList runat=server id="SelectionList1" 
      OnItemDataBind="TaskSummary" SelectType="CheckBox"/>
   <mobile:Label runat="server" id="Label2" Font-Italic=true 
      ForeColor=Red />
</mobile:Form>

See Also

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