ASP.NET Viewstate bug?

M

michela rossi

Hi,

Don't know if anyone can help:

We have a web control that shows a collection as a series of
checkboxes. Preselected items are passed in and the user is free to
change the checked items. This is then read out of a property and
saved in the database.

However there is a problem persisting the state of the control between
postbacks. The user-selected items are stored in a class that
derives from arraylist, called UpdateAwareArrayList, which is just
aware of any changes made to it.


I'm storing this class in viewstate with the following code (in
VB.net)


Protected Overrides Function SaveViewState() As Object
Dim baseState As Object = MyBase.SaveViewState()
Dim CombinedStates(1) As Object
CombinedStates(0) = baseState
CombinedStates(1) = CType(arSelectedItems, UpdateAwareArrayList)
Return CombinedStates
End Function


The class is declared as (in C#):

[Serializable()]
public class UpdateAwareArrayList : ArrayList, ISerializable

That seems to work, however when I try and get the class out of
viewstate, whats returned is a class of type ArrayList, not
UpdateAwareArrayList. I don't know why. As an attempt to work around
this I created a temporary arraylist to take the viewstate read, and
added its members to a variable of the right class:


Protected Overrides Sub LoadViewState(ByVal savedState As Object)

If Not (savedState Is Nothing) Then

Dim myState As Object() = CType(savedState, Object())
If Not (myState(0) Is Nothing) Then
MyBase.LoadViewState(myState(0))
End If

Dim arTemp As ArrayList
If Not (myState(1) Is Nothing) Then arTemp = myState(1)
If arTemp.Count > 0 Then
arSelectedItems = New UpdateAwareArrayList()
Dim intTempCounter As Int32
For intTempCounter = 0 To arTemp.Count - 1
arSelectedItems.Add(arTemp(intTempCounter))
Next
End If

End If
End Sub


This works for the first postback, however on the second I get a
corrupt viewstate error on the page.

Thanks, Michela.
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top