Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Generic StateManagedCollection?

One example is described here. But the author apparently forgot to include the code for download.

Another example is shown here. However, this one doesn't quite work (as described in comments).

How do you do this correctly?

like image 835
Larsenal Avatar asked Dec 12 '08 22:12

Larsenal


1 Answers

The second example you found almost works, it's just missing a little bit. All that was needed was 2 methods in the main control.

Add this code to the AppointmentControl.cs file and it will work.

protected override object SaveViewState()
{
    if (appointments != null)
        return appointments.SaveViewState();
    return null;
}

protected override void LoadViewState(object savedState)
{
    appointments = new AppointmentCollection();
    appointments.LoadViewState(savedState);
}

The code in the example site was pretty decent. It implemented all of the interfaces it should have and did a pretty good job. Where it fell apart was that, despite having all of the code it needed in the abstract bits, that didn't matter because the interfaces weren't referenced in the places they needed to be.

The collection classes being used had nothing "special" about them, other than implementing a few interfaces. The framework won't automatically call these methods. The framework will however call the overridden methods I wrote above, which you need to implement in order for your control to save the elements in the collection. As long as you call them, everything will work.

like image 84
Dan Herbert Avatar answered Sep 23 '22 00:09

Dan Herbert