Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Repeater ItemCreated vs ItemDataBound

Reply
Thread Tools

Repeater ItemCreated vs ItemDataBound

 
 
=?Utf-8?B?S2V2aW4gSGVjdG9y?=
Guest
Posts: n/a
 
      04-11-2004
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


 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      04-11-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ItemCreated V/s. ItemDataBound rn5a@rediffmail.com ASP .Net 3 09-20-2007 08:13 AM
Why Repeater.ItemCreated is fired before Page_Load on postback Philippe Camus ASP .Net 2 03-30-2006 12:57 PM
Using ItemCreated for a Repeater in VB? jason.gladstone@gmail.com ASP .Net 3 03-29-2006 06:26 AM
Repeater - ItemDataBound and ItemCreated Dennis E. Jones, Jr. ASP .Net 4 11-18-2005 05:45 PM
Repeater/ViewState: how to preserve properties on controls set on ItemCreated? Thomas Nielsen [AM Production A/S] ASP .Net Web Controls 2 12-02-2003 10:04 AM



Advertisments