views:

6902

answers:

9

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.

+1  A: 

I believe you'll need to add the UserControl to the PlaceHolder during the Init phase of the page life cycle, in order to get the ViewState to be filled in by the Load phase to read those values. Is this the order in which you're loading those?

bdukes
A: 

You have to create your controls in the Page_PreInit event handler. The ASP.NET server control model is tricky; you have to fully understand the page lifecycle to do it right.

Will
A: 

As others have said, any form of control manipulation must be done before viewstate is created.

Here is a good link on the page lifecycle to help you out:

http://msdn.microsoft.com/en-us/library/ms178472.aspx

FlySwat
+5  A: 

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.

icelava
I've found that the control has to be re-created with the same ID. In some situations it seems it's not the same... so I'm cheating and hard-coding it, which works but is incredibly inelegant.
Jeremy McGee
+1  A: 

Ensure you are defining your dynamic controls at the class level and adding them to the ASP container:

Private dynControl As ASP.MyNamespace_MyControl_ascx

And when you instantiate the control, ensure you call LoadControl so the object is added properly:

dynControl = CType(LoadControl("~/MyNamespace/MyControl/MyControl.ascx"), ASP.MyNamespace_MyControl_ascx)
Chris Porter
A: 

We have experienced the same thing and have handled it by using ghost controls on page_load that have the exact same .ID and then the post back picks up the events and the data. As others said it's the dynamic adding of the control after the init stages that the state is built already and controls added after aren't stored.

Hope this helps a bit.

osp70
A: 

I also want to add that I've seen user controls work the way that you'd expect them to just by setting the Control.ID property at run time. If you do not set the ID, items may get built in a different order and work oddly.

proudgeekdad
+2  A: 

I figured out yesterday that you can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.

Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    MyBase.LoadViewState(savedState)
    If IsPostBack Then
        CreateMyControls()
    End If
End Sub
Middletone
Nice one - it seems counter-intuitive that we create the controls /after/ ViewState is loaded - but it works just nicely.
Jeremy McGee
A: 

But hey what if you have a save button and clicked save?! The new data that you entered in the controls will be lost.

Cassini