Repeater ItemCreated vs ItemDataBound

Discussion in 'ASP .Net' started by =?Utf-8?B?S2V2aW4gSGVjdG9y?=, Apr 11, 2004.

  1. Hi. Can anyone explain why, when setting the Text of a Label control in the ItemCreated event of a Repeater (example below), the Text value is NOT persisted to ViewState? I know this can be fixed by using the ItemDataBound event, but I don't understand why.

    here's a page snippet:

    <asp:Repeater runat="server" id="myRepeater"
    OnItemCreated="myRepeater_ItemCreated">
    ...
    <ItemTemplate><tr><td><asp:Label id="myLabel" runat="server" /></td></tr></ItemTemplate>
    ...
    </asp:Repeater>

    and here's a simple ItemCreated event:

    protected void myRepeater_ItemCreated(object sender, RepeaterItemEventArgs
    e)
    {
    foreach(Control ctrl in e.Item.Controls)
    {
    if(ctrl is Label)
    {
    DataRowView r=(DataRowView)e.Item.DataItem;
    ((Label)ctrl).Text=r["MY_FIELD"].ToString();
    }
    }
    }

    Thanks

    KH
    =?Utf-8?B?S2V2aW4gSGVjdG9y?=, Apr 11, 2004
    #1
    1. Advertising

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

    Teemu Keiski Guest

    Hi,

    because in ItemCreated RepeaterItem is not yet added to Repeaters
    Controls/Items collection, which means its child controls are neither.
    Generally, Control lifecycle (Begin ViewState tracking is one phase in the
    lifecycle) starts after Control is added to the Controls collection. So it
    means state is tracked & saved for the changes made *after* control is added
    to the Controls collection.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke




    "Kevin Hector" <> wrote in message
    news:...
    Hi. Can anyone explain why, when setting the Text of a Label control in the
    ItemCreated event of a Repeater (example below), the Text value is NOT
    persisted to ViewState? I know this can be fixed by using the ItemDataBound
    event, but I don't understand why.

    here's a page snippet:

    <asp:Repeater runat="server" id="myRepeater"
    OnItemCreated="myRepeater_ItemCreated">
    ...
    <ItemTemplate><tr><td><asp:Label id="myLabel" runat="server"
    /></td></tr></ItemTemplate>
    ...
    </asp:Repeater>

    and here's a simple ItemCreated event:

    protected void myRepeater_ItemCreated(object sender, RepeaterItemEventArgs
    e)
    {
    foreach(Control ctrl in e.Item.Controls)
    {
    if(ctrl is Label)
    {
    DataRowView r=(DataRowView)e.Item.DataItem;
    ((Label)ctrl).Text=r["MY_FIELD"].ToString();
    }
    }
    }

    Thanks

    KH
    Teemu Keiski, Apr 11, 2004
    #2
    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. Dennis E. Jones, Jr.

    Repeater - ItemDataBound and ItemCreated

    Dennis E. Jones, Jr., Nov 18, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    15,910
    Dennis E. Jones, Jr.
    Nov 18, 2005
  2. Replies:
    3
    Views:
    1,912
  3. Philippe Camus
    Replies:
    2
    Views:
    2,507
    Philippe Camus
    Mar 30, 2006
  4. Replies:
    3
    Views:
    514
    Eliyahu Goldin
    Sep 20, 2007
  5. Thomas Nielsen [AM Production A/S]

    Repeater/ViewState: how to preserve properties on controls set on ItemCreated?

    Thomas Nielsen [AM Production A/S], Dec 1, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    357
    Thomas Nielsen [AM Production A/S]
    Dec 2, 2003
Loading...

Share This Page