2
votes

I'm dynamically loading User Controls into a div, which I need to preserve on postback in order to call a Save method once the user is done editing them. The div and all the User Controls have the EnableViewState = True.

ASPX Div Declaration

<div id="dynamicDiv" runat="server" enableviewstate="true">
</div>

Code Behind

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (Not IsPostBack) Then
        'Implement some logic to load the User Controls using LoadControl
        'Set all the userControl properties (including EnableViewState = True)
        'Call a method in the user control to load it's content

        Me.dynamicDiv.Controls.add(userControl)
    End If
End Sub

Protected Sub Save(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
    For Each t As userControlType In Me.dynamicDiv.Controls
        t.Save()
    Next
End Sub

So... the controls load fine, but I can't find a way to preserver them on postback, I can't reload them because the user has already input data into the usercontrol (which is what I need to save).

P.S. I tried adding them to a list and then adding the list to the ViewState, but I haven't been able to properly serialize the control. I implemented ISerializable for the userControl's code behind but then it says the ASCX is mot marked as serializable with

Type 'ASP.controls_userControlType_ascx' in Assembly 'App_Web_pn5vxhpw, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

2
You need to recreate your UserControls also if IsPostback. - Tim Schmelter
The thing is that the recreated control won't have any of the user's input. - PedroC88
If that would be true, no ASP.NET Control could retain values after postback. You only need to assign the same IDs as before and add them in Page_Load at the latest. If you know the number of controls to create(which could be stored in ViewState) you can derive the ID from the counter variable by appending it to the control-id. - Tim Schmelter
@TimSchmelter I'm new to ASP so if could expand that a bit further it would be great. Also I noticed that the controls, after postback, are lost (they disappear), so perhaps there's something wrong with the way I'm using the ViewState. - PedroC88
There are plenty of questions here on SO on this topic, even if i'm only searching my own answers i'll find a lot. - Tim Schmelter

2 Answers

5
votes

Dynamic controls need to be added durning page init not page load. Then they will be picked up by viewstate. And they need to be created every time. Ispostback is not needed when creating dynamic controls.

2
votes

Copied from comments:

You need to recreate your UserControls also if IsPostback.

You only need to assign the same IDs as before and add them in Page_Load at the latest. If you know the number of controls to create(which could be stored in ViewState) you can derive the ID from the counter variable by appending it to the control-id.