Bug with nested repeaters. Item events are called twice per item

Discussion in 'ASP .Net' started by =?Utf-8?B?SmFtZXMgR2V1cnRz?=, Mar 15, 2007.

  1. 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
    (http://www.biasecurities.com/files/folders/5113/download.aspx):

    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>
    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...
    =?Utf-8?B?SmFtZXMgR2V1cnRz?=, Mar 15, 2007
    #1
    1. Advertising

  2. =?Utf-8?B?SmFtZXMgR2V1cnRz?=

    bruce barker Guest

    Re: Bug with nested repeaters. Item events are called twice peritem

    your are not checking the item type to see if its a Item or AlternateItem

    -- bruce (sqlwork.com)

    James Geurts wrote:
    > 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
    > (http://www.biasecurities.com/files/folders/5113/download.aspx):
    >
    > 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>
    > 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...
    bruce barker, Mar 16, 2007
    #2
    1. Advertising

  3. Re: Bug with nested repeaters. Item events are called twice per i

    I don't see how that even applies to this situation. item events should only
    be called once per item regardless of if it is an Item or AlternateItem.

    Anyone from MS want to help with this?

    Thanks

    Jim

    "bruce barker" wrote:

    > your are not checking the item type to see if its a Item or AlternateItem
    >
    > -- bruce (sqlwork.com)
    >
    > James Geurts wrote:
    > > 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
    > > (http://www.biasecurities.com/files/folders/5113/download.aspx):
    > >
    > > 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>
    > > 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...

    >
    =?Utf-8?B?SmFtZXMgR2V1cnRz?=, Mar 16, 2007
    #3
  4. Hello? Anyone have an answer for this?

    "James Geurts" wrote:

    > 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
    > (http://www.biasecurities.com/files/folders/5113/download.aspx):
    >
    > 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>
    > 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...
    =?Utf-8?B?SmFtZXMgR2V1cnRz?=, Mar 24, 2007
    #4
  5. Got a reply 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...


    "James Geurts" wrote:

    > 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
    > (http://www.biasecurities.com/files/folders/5113/download.aspx):
    >
    > 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>
    > 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...
    =?Utf-8?B?SmFtZXMgR2V1cnRz?=, Mar 28, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul

    Nested datagrid/list/repeaters

    Paul, Mar 3, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    396
  2. =?Utf-8?B?TWFjaGk=?=

    nested Repeaters with javascript checkboxes

    =?Utf-8?B?TWFjaGk=?=, Apr 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    521
    =?Utf-8?B?TWFjaGk=?=
    Apr 16, 2004
  3. =?Utf-8?B?TWFjaGk=?=

    Nested Repeaters

    =?Utf-8?B?TWFjaGk=?=, Apr 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    420
  4. =?Utf-8?B?a2V0dGNo?=

    nested repeaters

    =?Utf-8?B?a2V0dGNo?=, Apr 26, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    364
    =?Utf-8?B?a2V0dGNo?=
    Apr 26, 2004
  5. =?Utf-8?B?UmFqZWV2?=

    Page Submission Events are called twice

    =?Utf-8?B?UmFqZWV2?=, May 17, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    330
    =?Utf-8?B?UmFqZWV2?=
    May 17, 2006
Loading...

Share This Page