Evento ItemDataBind (List)

Si verifica quando un elemento di un controllo List è associato a dati.

public event ListDataBindEventHandler ItemDataBind

Osservazioni

Quando un elemento di un controllo List viene creato e associato a dati, il gestore eventi imposta le proprietà dell'elemento di elenco utilizzando espressioni arbitrarie.

Un elemento di un controllo List è di tipo MobileListItem.

L'evento viene generato al momento di associare ciascun elemento dell'origine dati. Si tratta del momento migliore per filtrare o raggruppare gli elementi dell'insieme MobileListitemCollection utilizzando la proprietà Text o Value.

Esempio

Nell'esempio seguente viene mostrato come rilevare l'evento ItemDataBind. La proprietà DataValueField specifica quale azione viene eseguita.

<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.
   List1.DataValueField = "Status"
   List1.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 List to the array.
   List1.DataSource = arr
   List1.DataBind()
   
End If

End Sub


Sub TaskSummary(sender As [Object], e As ListDataBindEventArgs)
   Select Case e.ListItem.Value
      Case "Done"
         i += 1
      Case "Scheduled"
         j += 1
      Case "Pending"
         k += 1 
         
   End Select
   
   Label2.Text = "You have " + i.ToString() + " tasks done, " + j.ToString() + " tasks scheduled, and " + k.ToString() + " tasks Pending."
End Sub


Sub TaskStatus(sender As [Object], e As ListCommandEventArgs)

   Label3.Text = e.ListItem.Text + " is " + e.ListItem.Value

End Sub

<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>
   

[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>

Vedere anche

Classe ListDataBindEventArgs (SelectionList) | Classe ObjectList | classe ObjectListDataBindEventArgs | classe SelectionList | Metodo OnItemDataBind

Si applica a: classe List