jump to navigation

Repeaters and Lost Data After Postback (Viewstate) October 8, 2009

Posted by codinglifestyle in ASP.NET, CodeProject.
Tags: , ,

Test question: I have a form which binds data to a Repeater on PageLoad.  The Repeater’s ItemTemplate contains a TextBox and Checkbox.  On postback the data is lost.  What’s wrong?

You may have found this page if you have been googling the following:

  • repeater postback lost data
  • dynamic control postback viewstate
  • data lost on postback

The problem is the Repeater is a dynamic control.  If you are binding in the codebehind, which we typically are, you have to realize that the textbox and checkboxes do not existuntil you DataBind().  Keeping that in mind, we should ask ourselves what is the order of execution of ViewState.  I think we can start to formulate our answer to the question above by realizing where I DataBind() and create my controls in relation to the workings of ViewState is why our data is lost on postback.

So, the answer: PageLoad is the wrong place to bind a Repeater or setup a dynamic control.  It is too late.  ViewState has already tried to resync your controls but they weren’t created yet.

What about the answer you want?  Stop toying with me and tell me the answer I hear you say.  Try OnInit().  If you bind there you’re controls will exist in time for ViewState to operate normally.

protected override void OnInit(EventArgs e)

I would have also accepted Repeaters are the evil frogspawn of Satan and should never be used unless you find peeling off your fingernails with rusty pliers appealing.

ref: http://weblogs.asp.net/ngur/archive/2004/05/17/133340.aspx, http://aspnet.4guysfromrolla.com/articles/092904-1.aspx



1. Ivan - December 29, 2009

You saved my day. Thanks a lot!

2. Ben - April 20, 2010

THANK YOU!!!!! This was driving me crazy. I was trying to add all kinds of session code to figure out why the repeater databind wasn’t working on the page load after a postback occurred.


3. Torbjörn Stavås - September 28, 2010

Oh my god. This is just unvalueable information. So many incorrect suggestions you get if you google on this, but your info was spot on!

Thanks a bunch!!!

// Regards, Torbjörn

4. Erica - December 10, 2010

Thank you! This absolutely consumed my day yesterday. I found your post at 4:50 and actually felt like I accomplished something by 5:00.

5. Sebastian Patri - February 9, 2011

Thanks a lot friend!! You save my work!

6. mraza - August 16, 2011


7. Derek - February 7, 2012

Thank you so much, I lost way too much time with code examples that didn’t point this out.

8. Joao - May 2, 2012

Thanks very much!!!:) A simple explanation!:) You save my job!:) Thanks!! From Portugal with Love:)

9. Flo - July 12, 2012

Same thing for me… It works perfectly ! I can now go to bed…

10. Barış ATEŞ - August 15, 2012

you saved my life ! atesbaris

11. None - December 11, 2012

thanks! in my case onInit() didn’t do the job however you have pointed me in the right direction and I realized that the code should be in OnDataBinding
protected override void OnDataBinding(EventArgs e)
//My Code goes here

(data binding for a control in a repeater)

12. Manasi - January 22, 2013

Thanks .. It was really helpful… I was struggling to retain the values

13. Stefano - October 14, 2013

Spot on, thanks a lot for the info!

14. swetha - October 22, 2013

thanku so much… i lost so much of time looking was answers else where…

15. Rob - March 27, 2014

Thanks 🙂 And why are repeaters the evil frogspawn of Satan?

codinglifestyle - March 28, 2014

Due to their dynamic nature, they add an extra layer of complexity when dealing with custom controls. A good understanding of the ASP.NET Page Lifecycle is required to get everything working in practice.

16. UK - May 6, 2016

This worked perfectly in my solution:

protected override void OnInit(EventArgs e)



If you have a call to DataBind() in your page’s Page_Load method, the user-entered values (e.g. in a TextBox inside the repeater) were lost.

So it was important in my case to not call DataBind.

17. Virendrea - August 3, 2016

Thanks a lot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: