Repeater ItemCreated vs ItemDataBound

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
 
T

Teemu Keiski

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top