Panel View State and ReferenceConverter

Discussion in 'ASP .Net Web Controls' started by Scott Wisniewski, Jan 8, 2004.

  1. 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
     
    Scott Wisniewski, Jan 8, 2004
    #1
    1. Advertising

  2. 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.

    "Scott Wisniewski" <> wrote in message
    news:%...
    > 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
    >
    >
     
    Scott Wisniewski, Jan 8, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harshdeep Mehta
    Replies:
    1
    Views:
    2,093
    Teemu Keiski
    Jul 8, 2004
  2. Replies:
    2
    Views:
    1,746
  3. Yogesh Parab
    Replies:
    1
    Views:
    3,187
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 2, 2006
  4. Robin Wenger
    Replies:
    0
    Views:
    2,955
    Robin Wenger
    Aug 18, 2011
  5. Parthiv Joshi
    Replies:
    1
    Views:
    751
    Samuel L Matzen
    Jul 6, 2004
Loading...

Share This Page