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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use SaveControlState and LoadControlState https://docs.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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x