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.
 
M

michela rossi

Cor said:
Hi Michela,
May I ask where you store the data when you do the postback?
Cor

I'm trying to store the data in the viewstate on the browser - the form is
posting back because of other controls: eg a checkbox control with
auto-postback set to true. When this posts back when the checkbox is
clicked, the control with the problem needs to store its state in viewstate,
otherwise the state is lost.
 
C

Cor

Hi Michela,
Did you read this in the documentation.
Note The data must be in a format compatible with view state. For example,
to store a dataset in view state, you should first convert it to a string
representation.
I did not see any conversion in your code to string (there are examples
given in the documentation with streamreader and stringreader.)
(The only format I could find when I was reading this fast usable to
viewstate (I don't know if there are more, but it can be the problem).
Succes
Cor
 
M

michela rossi

The UpdateAwareArrayList class implements Iserializable, and has the
following 2 methods



public UpdateAwareArrayList(SerializationInfo info,
StreamingContext context)

{

int intItemCount = (Int32)
info.GetValue("intItemCount", typeof(Int32));

for (int intItemCounter = 0 ; intItemCounter <
intItemCount; intItemCounter++)

{

this.Add(info.GetValue("Item" +
intItemCounter, typeof(Object)));

}

blnStartMonitoring = (bool)
info.GetValue("blnStartMonitoring", blnStartMonitoring.GetType());

blnChanged = (bool) info.GetValue("blnChanged",
blnChanged.GetType());



}



public void GetObjectData(SerializationInfo info,
StreamingContext context)

{

info.AddValue("intItemCount", this.Count);

for (int intItemCounter = 0; intItemCounter <
this.Count; intItemCounter++)

{

info.AddValue("Item" +
intItemCounter.ToString(), this[intItemCounter]);

}

info.AddValue("blnStartMonitoring",
blnStartMonitoring);

info.AddValue("blnChanged", blnChanged);

}





This *should* store the state automatically when the class is added to
the viewstate, or so I had hoped.
 

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

Top