ASP.NET Viewstate bug?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michela rossi

    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.
  • Cor

    #2
    Re: ASP.NET Viewstate bug?

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


    Comment

    • michela rossi

      #3
      Re: ASP.NET Viewstate bug?

      "Cor" <non@non.com> wrote in message news:<3f558780$ 0$15354$48b97d0 1@reader20.wxs. nl>...[color=blue]
      > Hi Michela,
      > May I ask where you store the data when you do the postback?
      > Cor[/color]

      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.

      Comment

      • Cor

        #4
        Re: ASP.NET Viewstate bug?

        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


        Comment

        • michela rossi

          #5
          Re: ASP.NET Viewstate bug?

          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.

          Comment

          • Cor

            #6
            Re: ASP.NET Viewstate bug?

            Michela,
            Did you look here

            It is so nice decribed that I think you can do it yourself.
            If problems I will try to help you further OK?
            Cor


            Comment

            Working...