Search code examples
asp.netpropertiesfailed-to-load-viewstate

ASP.NET, User Control and ViewState lost the correct data into try get into the user control event


Good afternoon, I have an ASP.NET web page and I created a user control that has some properties that I need to always be retrievable. ViewState was added to keep information, but the drawback now is that when adding the same control on the same asp.net page and from within a function of the control I want to retrieve the value of the property, this will retrieve the last value entered in the ViewState .

Beforehand thank you very much.

<PersistenceMode(PersistenceMode.InnerProperty), TemplateInstance(TemplateInstance.Single)>
Public Property GuardadoTemporal() As Boolean
    Get
        If ViewState("GuardadoTemporal") Is Nothing Then
            Return false
        Else
            Return CBool(ViewState("GuardadoTemporal"))
        End If
    End Get
    Set(ByVal value As Boolean)
        ViewState("GuardadoTemporal") = value
    End Set
End Property

example for use


Solution

  • Use SaveControlState and LoadControlState https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.control.savecontrolstate?view=netframework-4.8 This will save it into the controlstate for each control so you don't get user controls clobbering each others data. controlstate can't be turned off either