Repeater ItemCreated vs ItemDataBound

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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




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
 
Back
Top