Evento ItemDataBind (ObjectList)

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

public event ObjectListDataBindEventHandler ItemDataBind

Osservazioni

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

Un elemento di un controllo ObjectList è di tipo ObjectListItem.

Esempio

Nell'esempio seguente viene mostrato come rilevare l'evento ItemDataBind. È possibile utilizzare la proprietà ListItem o la proprietà DataItem dell'insieme ObjectListDataBindEventArgs per determinare ulteriori azioni. È possibile ad esempio aggiungere codice per ordinare il libro richiesto dal cliente se il valore della proprietà DataItem è 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>

Vedere anche

classe ObjectListDataBindEventArgs | Evento ItemDataBind (List) | Classe List | Classe ListDataBindEventArgs (SelectionList) | classe SelectionList | Classe ListDataBindEventArgs | Metodo OnItemDataBind | Classe ObjectList