Viewstate and preserving data on postbacks

J

Joe Abou Jaoude

hi,

I just want simply to preserve some data in my usercontrol on postbacks.

so I added this property in my UserControl

Protected Property MyProperty() As String
Get
If Not ViewState("xxx") Is Nothing Then
Return ViewState("xxx")
Else
Return ""
End If
End Get
Set(ByVal value As String)
ViewState("xxx") = value
End Set
End Property

However I lost the viewstate value on postbacks. I checked that my
usercontrol has enabledviewstate=true.
What I m doing wrong ?
If I replace in the code ViewState by Session , the data is preserved,
however session won't work for me.(case of two pages using the
usercontrol in the same session)

Thank you
 
R

Riki

Joe said:
hi,

I just want simply to preserve some data in my usercontrol on
postbacks.

so I added this property in my UserControl

Protected Property MyProperty() As String
Get
If Not ViewState("xxx") Is Nothing Then
Return ViewState("xxx")
Else
Return ""
End If
End Get
Set(ByVal value As String)
ViewState("xxx") = value
End Set
End Property

However I lost the viewstate value on postbacks. I checked that my
usercontrol has enabledviewstate=true.
What I m doing wrong ?
If I replace in the code ViewState by Session , the data is preserved,
however session won't work for me.(case of two pages using the
usercontrol in the same session)

Your code seems correct.

First, make sure that any code that initializes your property before
postback
is not executed again after postback.

Then, is your user control by any chance created dynamically (with New)?
In that case, make sure it gets the same id before and after the postback.
If not, viewstate is not preserved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top