Panel View State and ReferenceConverter

  • Thread starter Scott Wisniewski
  • Start date
S

Scott Wisniewski

Please Help!!!!

I am having a frustrating problem creating a control that extends the Panel
class, and was hoping someone here could help. My control has its own fields
in addition to the ones provided by the panel class that must be serialized
to view state. To facilitate this I created a class that contains the fields
defined in my class and a reference to the base class view state (the return
value of base.SaveViewState()). It essentially looks like this:

enum EnEnumType {
Foo,
Bar
}

public class APanelControl : Panel {
protected AnEnumType enumValue_;

protected class ViewStateRecord_ {
public AnEnumType value;
public object parentViewState;

public ViewStateRecord_(AnEnumType value, object parentViewState) {
this.value = value;
this.parentViewState = parentViewState;
}
}

protected override object SaveViewState() {
return new ViewStateRecord_(enumValue_, base.SaveViewState());
}

protected override void LoadViewState(object savedState) {
ViewStateRecord typedState = savedState as ViewStateRecord;
enumValue_ = typedState.value;
base.LoadViewState(typedState.parentViewState);
}
}


However this raises an exception when saving the view state:

"The type 'APanelControl+ViewStateRecord_' must be marked as Serializable or
have a TypeConverter other than ReferenceConverter to be put in viewstate."

The problem appears to be caused by the object reference to the parent view
state (or as the error message indicates, by the use of ReferenceConverter).
If I do not serialize the parentViewState, or don't override the view state
methods, I do not receive an error.

My question is, how do I save the parent view state?
Is there a type converter I can use?

Inspecting the return value of base.SaveViewState() in the debugger
indicates that it is a Pair instance, composed of several other Pair
instances, some of which contain null values. I believe (speculatively) that
these null values are what cause problems for ReferenceConverter. I checked
MSDN , and there does not appear to be any public classes that derive from
System.ComponentModel.TypeConverter that are associated with a Pair or a
Panel instance, and the documentation for the panel class does not discuss
view state.

Does anyone have any idea as to how I can solve this?

Thanks


-Scott Wisniewski
 
S

Scott Wisniewski

I was able to solve the problem my self.
Rather than modifying the value returned by SaveViewState, I simply added an
entry into the ViewState property, and then returned base.SaveViewState()
unaltered. This worked without error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top