ASP.Net: User controls added to placeholder dynamically cannot retrieve values

Steve Horn picture Steve Horn · Sep 19, 2008 · Viewed 17.6k times · Source

I am adding some user controls dynamically to a PlaceHolder server control. My user control consists of some labels and some textbox controls.

When I submit the form and try to view the contents of the textboxes (within each user control) on the server, they are empty.

When the postback completes, the textboxes have the data that I entered prior to postback. This tells me that the text in the boxes are being retained through ViewState. I just don't know why I can't find them when I'm debugging.

Can someone please tell me why I would not be seeing the data the user entered on the server?

Thanks for any help.

Answer

icelava picture icelava · Sep 19, 2008

This is based on .NET v1 event sequence, but it should give you the idea:

  • Initialize (Init event)
  • Begin Tracking View State (checks if postback)
    • Load View State (if postback)
    • Load Postback Data (if postback)
  • Load (Load event)
    • Raise Changed Events (if postback)
    • Raise Postback Events (if postback)
  • PreRender (PreRender event)
  • Save View State
  • Render
  • Unload (Unload event)
  • Dispose

As you can see, the loading of ViewState data back to the controls happen before the Load event. So in order for your dynamically-added controls to "retain" those values, they have to be present for the ASP.NET page to reload the values in the first place. You would have to re-create those controls at the Init stage, before Load View State occurs.