How to use a viewstate'd object as a datasource for controls on a user control

Posted by user557325 on Stack Overflow See other posts from Stack Overflow or by user557325
Published on 2010-12-29T16:51:23Z Indexed on 2010/12/29 16:53 UTC
Read the original article Hit count: 433

I've got a listview on a control. Each row comprises a checkbox and another listview.

The outer listview is bound to a property on the control (via a method call, can't set a property as a SelectMethod on an ObjectDataSource it would appear) which is lazy loaded suchly:

    Public ReadOnly Property ProductLineChargeDetails() As List(Of WebServiceProductLineChargeDetail)
    Get
        If ViewState("WSProductLineChargeDetails") Is Nothing Then
            ViewState("WSProductLineChargeDetails") = GetWebServiceProductLineChargeDetails()
        End If

        Return DirectCast(ViewState("WSProductLineChargeDetails"), Global.System.Collections.Generic.List(Of Global.MI.Open.WebServiceProductLineChargeDetail))
    End Get
End Property

The shape of the object referenced by the data source is something like this:

(psuedocode)

Product
{
 bool Licenced;
 List<Charge> charges;
}

Charge
{
 int property1;
 string property2;
 bool property3
 .
 .
 .
}

The reason for the use of viewstate is this:

When an one of the checkboxes on one of the outer list view rows is checked or unchecked I want to modify the object that the ODS represents (for example I'll add a couple of Charge objects to the relevant Product object) and then rebind.

The problem I'm getting is that after every postback (specifically after checking or unchecking one of the rows' checkbox) my viewstate is empty. Thiss means that any changes I make to my viewstate'd object is lost.

Now, I've worked out (after much googling and reading, amongst many others, Scott Mitchel's excellent bit on ViewState) that during initial databinding IsTrackingViewState is set to false. That means, I think, that assigning the return from GetWebServiceProductLineChargeDetails() to the ViewState item in my Property Get during the initial databind won't work.

Mind you, even when the IsTrackingViewState is true and I call the Property Get, come the next postback, the viewstate is empty.

So do you chaps have any ideas on how I keep the object referenced by the ObjectDataSource in ViewState between postbacks and update it and get those changes to stay in ViewState? This has been going on for a couple of days now and I'm getting fed up!

Cheers in advance

Steve

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about custom-controls