locked
Nested repeater performance issue. Item events being called twice RRS feed

  • Question

  • User1127880124 posted

    I was looking at some weird performance issues that I've been experiencing lately and discovered a major issue with nested repeater objects.  It's possible that I'm doing this wrong, however.... If that's the case, please let me know :)

    On my page, I have one repeater that contains a literal control and a nested repeater.  The nested repeater contains a literal control.  Both repeaters are databound with only one object (string).  But... and this is the crappy part, the nested repeater's events are fired twice.  How do I know this?  I setup global private counter variables that get incremented on the repeater's ItemDataBound event.  The outer repeater is correct and only calls the ItemDataBound event once for each item, but the inner nested repeater calls it's ItemDataBound event twice for each item. 

    It's worth mentioning that I did set AutoEventWireup to false, just in case...

    You can review the markup and code or download the code:

    Default.aspx only has:

            <asp:repeater ID="OuterRepeater" runat="server">
                <ItemTemplate>
                        Outer ItemDataBind function called <asp:Literal ID="MyText" runat="server" /> time(s)<br />

                        <asp:Repeater ID="InnerRepeater" runat="server">
                            <ItemTemplate>
                                &nbsp;&nbsp;Nested ItemDataBind function called <asp:Literal ID="Text" runat="server" /> time(s)<br />
                               
                            </ItemTemplate>
                        </asp:Repeater>   
                </ItemTemplate>
            </asp:repeater>

    Default.aspx.cs contains: 

            protected override void OnInit(EventArgs e)
            {
                OuterRepeater.ItemCreated += new RepeaterItemEventHandler(Outer_ItemCreated);
                OuterRepeater.ItemDataBound += new RepeaterItemEventHandler(Outer_ItemDataBound);
                this.Load += new EventHandler(Page_Load);
                base.OnInit(e);
            }

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    List<string> myList = new List<string>();
                    myList.Add("1");
                    OuterRepeater.DataSource = myList;
                    OuterRepeater.DataBind();
                }
            }

            void Outer_ItemCreated(object sender, RepeaterItemEventArgs e)
            {
                Repeater repeater = (Repeater)e.Item.FindControl("InnerRepeater");
                if (repeater != null)
                {
                    List<string> myList = new List<string>();
                    myList.Add("1.1");
                    repeater.ItemDataBound += new RepeaterItemEventHandler(Inner_ItemDataBound);
                    repeater.DataSource = myList;
                    repeater.DataBind();
                }
            }

            int outerCount = 0;

            void Outer_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                outerCount++;
                string myValue = (string)e.Item.DataItem;

                Literal text = (Literal)e.Item.FindControl("MyText");
                if (text != null)
                {
                    text.Text = outerCount.ToString();
                }
            }

            int innerCount = 0;

            void Inner_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                innerCount++;
                string myValue = (string)e.Item.DataItem;
                Literal text = (Literal)e.Item.FindControl("Text");
                text.Text = innerCount.ToString();
            }

    So at this point, I'm just confused... is this a bug with asp.net?  if so... damn, it seems pretty major, imo.  Any help would be appreciated...

    Thursday, March 15, 2007 11:20 AM

Answers

  • User1127880124 posted

    Got an answer from Microsoft:

    "The repeater calls databind on all its item children in the ItemDataBound event. So the inner repeater is databound once by the outer databound, and once by you in your Outer_ItemCreated event handler. To make the inner repeater bind just once, remove the call to DataBind in Outer_ItemCreated.

    Hope that helps someone seeing the same issue...
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 26, 2007 10:04 AM

All replies

  • User1127880124 posted
    I'd like to add that if I extract the nested repeater into its own ascx file, then everything works as expected.
    Thursday, March 15, 2007 12:32 PM
  • User1127880124 posted

    Got an answer from Microsoft:

    "The repeater calls databind on all its item children in the ItemDataBound event. So the inner repeater is databound once by the outer databound, and once by you in your Outer_ItemCreated event handler. To make the inner repeater bind just once, remove the call to DataBind in Outer_ItemCreated.

    Hope that helps someone seeing the same issue...
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 26, 2007 10:04 AM
  • User-2046974395 posted

     Nice one James, I've just been going back and forward on this one but your answer was exactly what I needed to know.

    Thursday, January 24, 2008 10:16 AM
  • User1541806237 posted

     Thanx! Been having the same problem.

    This doesn't help me much though, what if you want to perform some code in the ItemDataBound event? The code gets ignored if you remove the AddHandler. 

    Wednesday, May 27, 2009 9:00 AM