Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Responding to Events in Repeater Items

Posted by Ravi Varma Thumati on April 5, 2011

The Repeater control provides several events that you can program against. This allows you to run a custom routine whenever an event occurs. The following table lists the events supported by the Repeater control.

Event Description
ItemCommand Occurs when any button within a Repeater control is clicked. This event is often used to perform a task when a button is clicked in the control.
ItemCreated Occurs when an item is created in the Repeater control. This event is often used to modify the values of a record before it is displayed.
ItemDataBound Occurs after an item in the Repeater is data-bound but before it is rendered on the page. This event is often used to modify the values of a record before it is displayed.

Responding to the ItemCommand Event

 

If a Repeater control template includes a Button, LinkButton, or ImageButton Web server control, these buttons include a click event that can bubble up to the containing Repeater control. This allows you to define your own functionality for the Repeater control.

NOTE: Only controls that support a Command event can bubble their events to the Repeater control.

The following example illustrates a way to handle the ItemCommand event of a Repeater. Information about the selected item is displayed whenever any of the LinkButton controls within the Repeater is clicked.

The below snippet shows how to attach a handler for the event.

<asp:repeater id = "myRepeater" runat = "server" onItemCommand = "showItem" ... >

The below shows how the handler method is defined.

void showItem ( Object src, RepeaterCommandEventArgs e ) {  
   items.Text = e.Item.ItemType.ToString ( ) + " " + 
      e.Item.ItemIndex.ToString ( ) + ", " + 
      ( ( LinkButton ) e.CommandSource ).Text + "<br>";
}

 

Responding to the ItemCreated Event

The following example illustrates a way to handle the ItemCreated event of a Repeater. In this case, the sample simply retrieves and displays either the item type or data values of each RepeaterItem, in the order in which the items are created.

The below snippet shows how to attach a handler for the event.

<asp:repeater id = "myRepeater" runat = "server" onItemCreated = "showItem" ... >

The below shows how the handler method is defined.

void showItem ( Object src, RepeaterItemEventArgs e ) {
   if ( e.Item.ItemType == ListItemType.Item ||
         e.Item.ItemType == ListItemType.AlternatingItem ) {
      object [ ] dataitems = ( ( DataRowView ) e.Item.DataItem ).Row.ItemArray;
      items.Text += "<br>" + dataitems [ 0 ] + " " + dataitems [ 1 ] + " " + dataitems [ 2 ];
   }
   else {
      items.Text += "<br>" + e.Item.ItemType.ToString ( );
   }
}

 

Responding to the ItemDataBound Event

 

The following example illustrates a way to handle the ItemDataBound event of a Repeater. The page contains a Repeater control that is used to display a list of hotels based on user input.

In this case, the ItemDataBound event handler is used to accomplish the following:

  • obtain and set the ImageUrl, NavigateUrl, ToolTip and Attributes properties for the hotel image hyperlink;
  • obtain and set the Text, NavigateUrl, ToolTip and Attributes properties for the hotel name hyperlink;
  • obtain and set the ImageUrl, NavigateUrl, ToolTip and Attributes properties for the rating image hyperlink;
  • compute and set the average nightly rate.

The below snippet shows how to attach a handler for the event.

<asp:repeater id = "myRepeater" runat = "server" onItemDataBound = "showItem" ... >

The below shows how the handler method is defined.

void setListBindings ( Object src, RepeaterItemEventArgs e ) {
   if ( e.Item.ItemType == ListItemType.Item ||
         e.Item.ItemType == ListItemType.AlternatingItem ) {

      ... see example ...

   }
}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: