473,566 Members | 2,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET Viewstate bug?

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 UpdateAwareArra yList, 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.SaveView State()
Dim CombinedStates( 1) As Object
CombinedStates( 0) = baseState
CombinedStates( 1) = CType(arSelecte dItems, UpdateAwareArra yList)
Return CombinedStates
End Function
The class is declared as (in C#):

[Serializable()]
public class UpdateAwareArra yList : 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
UpdateAwareArra yList. 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(B yVal savedState As Object)

If Not (savedState Is Nothing) Then

Dim myState As Object() = CType(savedStat e, Object())
If Not (myState(0) Is Nothing) Then
MyBase.LoadView State(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 UpdateAwareArra yList()
Dim intTempCounter As Int32
For intTempCounter = 0 To arTemp.Count - 1
arSelectedItems .Add(arTemp(int TempCounter))
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.
Nov 20 '05 #1
5 2456
Cor
Hi Michela,
May I ask where you store the data when you do the postback?
Cor
Nov 20 '05 #2
"Cor" <no*@non.com> wrote in message news:<3f******* *************** *@reader20.wxs. nl>...
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.
Nov 20 '05 #3
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
Nov 20 '05 #4
The UpdateAwareArra yList class implements Iserializable, and has the
following 2 methods

public UpdateAwareArra yList(Serializa tionInfo info,
StreamingContex t context)

{

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

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

{

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

}

blnStartMonitor ing = (bool)
info.GetValue(" blnStartMonitor ing", blnStartMonitor ing.GetType());

blnChanged = (bool) info.GetValue(" blnChanged",
blnChanged.GetT ype());

}

public void GetObjectData(S erializationInf o info,
StreamingContex t context)

{

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

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

{

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

}

info.AddValue(" blnStartMonitor ing",
blnStartMonitor ing);

info.AddValue(" blnChanged", blnChanged);

}

This *should* store the state automatically when the class is added to
the viewstate, or so I had hoped.
Nov 20 '05 #5
Cor
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
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
21631
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter the results in the grid. Say you filter the grid for records that have a certain condition set to "NO" (in this case a checkbox). In this scenario...
3
2634
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event and this causes control B to be created, when this is done the VIEWSTATE is lost for CONTROL B. In the EVENT that causes CONTROL B to be created...
10
2247
by: neo | last post by:
hi, I am studying ASP.NET and have few questions - 1) The session ID and values of controls is stored in VIEWSTATE variable. So now when we put EnableViewState="false" in Page directive and disable the session state in Web.Config the VIEWSTATE variable is still maintained and stores some values. Can anyone tell what those values are for,...
1
1761
by: Simon | last post by:
Hi everyone, I have a quick question that I hope someone can help me with: I've made a user control that contains a text box and some validation functionality. This control has a few extra properties declared on it over and above that provided by the textbox. As I understand it, part of the process to make this work is, in the property...
7
2035
by: et | last post by:
I'm not sure I understand the use of the ViewState. Do I understand correctly that values of controls are automatically held in a hidden control called ViewState? If so, then why can't we get them, or how do we get that value? I always have to set the ViewState("FirstName") = txtBox.text myself before a postback is done, otherwise, the...
3
4554
by: RCS | last post by:
I have an app that I have different "sections" that I want to switch back and forth from, all while having the server maintain viewstate for each page. In other words, when I am on Page1.aspx and set textboxes, radio buttons, etc - that viewstate is fine. Then I have a linkbutton that does a Server.Transfer over to Page2.aspx. When I...
9
1856
by: Mark Broadbent | last post by:
Been a while since I've touched asp.net but one thing that always seems to fustrate me is the loss of state on variable declarations. Is there anyway (i.e. assigning an attribute etc) to instruct the server to remember a variables state *without* having to go through the rigmarole of saving and loading to and from the Session state manually or...
6
2058
by: hitendra15 | last post by:
Hi I have created web user control which has Repeater control and Linkbutton in ItemTemplate of repeater control, following is the code for this control On first load it runs fine but when page gets post back it gives following error Failed to load viewstate. The control tree into which viewstate is
6
4445
by: paul.hester | last post by:
Hi all, Does anyone know why the ViewState would be empty? When I'm receiving a postback, I can access a posted value using controlName.Value but not ViewState. I have EnableViewState set to true in my master page and content page. Thanks,
12
1904
by: Nick C | last post by:
Hi How can i reduce the viewstate for my asp.net application. It is getting very large now. What is a good solution? thanks N
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.