Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Repeaters and Lost Data After Postback (Viewstate)

, 30 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to solve the issue of lost data in Repeaters on postback.

Test question: I have a form which binds data to a Repeater on PageLoad. The Repeater’s ItemTemplate contains a TextBox and a 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 code-behind, which we typically are, you have to realize that the textbox and checkboxes do not exist until 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, your 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.

References:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chris_Green
Software Developer (Senior) Storm Technology Ltd.
Ireland Ireland
Formerly a C++ client developer, nowadays I’m all about C# and ASP.NET. I work for a consulting firm which specializes in .NET technologies. Over the years I have mastered some and played with many aspects of .NET.
 
Follow my blog as I catalogue the more arcane problems I encounter and their solutions at CodingLifestyle.com

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 30 Mar 2011
Article Copyright 2011 by Chris_Green
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid