Blog Stats
  • Posts - 4
  • Articles - 0
  • Comments - 13
  • Trackbacks - 5

 

The Bubbling Repeater Control

Originally I had this problem more than a year ago but recently while developing a fairly robust ASP.NET 1.1 user interface I encuntered it again.  This time though I was prepared and also thought that it was worthwhile writing a short post about.

You may have noticed that if you were to nest a Repeater control inside of another databound templated control, the ItemCommand() event does not fire for command buttons such as LinkButtons & ImageButttons that are inside of the Repeater.  The reason is that the repeater is not bubbling up the event to the container control.

To solve the problem you can simply create a new class that derives from the Repeater control and override the OnBubbleEvent() method.

Here's the sample code:

Public Class BubblingRepeater
   
Inherits
System.Web.UI.WebControls.Repeater
   
   
Protected Overrides Function OnBubbleEvent(ByVal sender As Object, ByVal e As System.EventArgs) As
Boolean
   
MyBase
.OnBubbleEvent(sender, e)

   
End
Function

End Class

Now to add this control to a web page you'll need a <%@ Register %> directive.  Here's a sample:

<%@ Register TagPrefix="MY" Namespace="MyNamespace" Assembly="MyAssembly" %>

Then instead of using an <asp:Repeater> tag in your code you insert a <MY:BubblingRepeater> tag like this:

<MY:BubblingRepeater>
   <ItemTemplate>
      Your content goes here...
   </ItemTemplate>

</MY:BubblingRepeater>

That's it!  Oh one last thing.  The IDE will give you a warning on the <ItemTemplate> tag saying that "The active schema does not suport the element 'ItemTemplate'".  Don't worry about that, it's more trouble to get rid of than it's worth.

Enjoy!


Feedback

# re: The Bubbling Repeater Control

Gravatar Thanks, thats a clear, concise explanation of the solution to a problem that I spent a couple hours trying to resolve. 1/23/2007 9:04 PM | rrostock

# re: The Bubbling Repeater Control

Gravatar In The Name of God

Hi

In my case I have a repeater inside a datalist. when i click on one of the items in repeater, I want the ItemCommand of both (Repeater and Datalist ) get fired. What can I do?

Thanks 6/12/2007 3:34 AM | M.Karimipour

# re: The Bubbling Repeater Control

Gravatar In The Name of God

Hi Again

I found my answer !!! by writing in this style in the OnBubbleEvent :

protected override bool OnBubbleEvent(object sender, EventArgs e)
{
base.OnBubbleEvent(sender, e);

return false;
}

false meanse triggering the parent event and base.On... means triggering its own .

Thanks for your post . It was great.

6/12/2007 4:43 AM | M.Karimipour

# re: The Bubbling Repeater Control

Gravatar You can also just add a handler for those events when the item is created...

rpt_Parent.ItemCreated += new RepeaterItemEventHandler(rptParent_ItemCreated);

...

private void rptParent_ItemCreated(object sender, RepeaterItemEventArgs e)
{
Repeater rptChild = (Repeater)e.Item.FindControl( "rptChild" );

if( rptChild != null )
rptChild.ItemCommand +=new RepeaterCommandEventHandler(rptChild_ItemCommand);

}

private void rptChild_ItemCommand(object source, RepeaterCommandEventArgs e)
{
m_alert.Message = e.CommandArgument + ":" + e.CommandName;
}


so you create an image button in that child repeater, put a commandname in there, and argument, and you're set 7/18/2007 8:49 AM | Sean

Post a comment





 

Please add 6 and 3 and type the answer here:

 

 

Copyright © Albert Raiani