PC Review


Reply
Thread Tools Rate Thread

ASP.NET Viewstate bug?

 
 
michela rossi
Guest
Posts: n/a
 
      2nd Sep 2003
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.
 
Reply With Quote
 
 
 
 
Cor
Guest
Posts: n/a
 
      3rd Sep 2003
Hi Michela,
May I ask where you store the data when you do the postback?
Cor


 
Reply With Quote
 
michela rossi
Guest
Posts: n/a
 
      3rd Sep 2003
"Cor" <(E-Mail Removed)> wrote in message news:<3f558780$0$15354$(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      3rd Sep 2003
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


 
Reply With Quote
 
michela rossi
Guest
Posts: n/a
 
      4th Sep 2003
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.
 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      4th Sep 2003
Michela,
Did you look here
http://msdn.microsoft.com/library/de...teproperty.asp
It is so nice decribed that I think you can do it yourself.
If problems I will try to help you further OK?
Cor


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Errors: Failed to load viewstate. & Validation of viewstate MAC failed. sck10 Microsoft ASP .NET 6 1st Sep 2006 06:59 PM
viewstate .net 1.1 vs 2.0 Simon Gorski Microsoft ASP .NET 2 3rd Feb 2006 09:04 AM
ViewState =?Utf-8?B?QWxleCBDLiBCYXJiZXJp?= Microsoft ASP .NET 4 27th Jan 2006 08:29 PM
Loading usercontrols, viewstate problem, slighly different from all others "viewstate uc problems" please help... ujjc001 Microsoft ASP .NET 0 27th Jul 2005 02:52 PM
Corrupted ViewState (Yes, another issue concerning viewstate) Ben Rush Microsoft ASP .NET 2 5th Dec 2003 04:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:36 PM.