我在我的程序中定义了一个自定义类集:
<Serializable()> Public Class cSimpleCategoriesCollection

end class然后在页面中通过ViewState保存该类集的实例Property curSimpleCategories() As cSimpleCategoriesCollection
            Get
                Return CType(viewstate("curSimpleCategories"), cSimpleCategoriesCollection)
            End Get
            Set(ByVal Value As cSimpleCategoriesCollection)
                viewstate("curSimpleCategories") = Value
            End Set        End Property
但是发生页面回传,如点击了一个按钮后,保存在viewstate中的实例就没有了。但是如果存在ViewState中的是普通类型的变量,如integer则可以保存得住,
请问是什么原因呢

解决方案 »

  1.   

    指定的转换无效。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误: 行 40:Return CType(viewstate("curSimpleCategories"), cSimpleCategoriesCollection) 
      

  2.   

    试一试
    将类继承ISerializable借口
    完成函数
    #region ISerializable 成员
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
    info.AddValue("Sender", _Sender) ;//变量
    } #endregion
    序列化我也不怎么懂,只是要将自己定义的类,在后台付给ViewState时遇到过不能付值
    用这个解决了,你不妨试一下
      

  3.   

    我已经将类<Serializable()> Public Class cSimpleCategoriesCollection
    而且可以将自定义类集的实例赋给viewstate,在页面没有回传的时候,也可以读取viewstate中的值。
    但是页面发生回传后,该值就没有了
      

  4.   

    我把
    ******************************************************************
    Property curSimpleCategories() As cSimpleCategoriesCollection
                Get
                    Return CType(viewstate("curSimpleCategories"), cSimpleCategoriesCollection)
                End Get
                Set(ByVal Value As cSimpleCategoriesCollection)
                    viewstate("curSimpleCategories") = Value
                End Set        End Property
    *************************************************************
    中使用viewstate改为使用Session就可以了
    只不过我想用viewstate,来保存?
      

  5.   

    大体知道原因了,你将实例序列化了,但是没有将其反序列
    这个就要用一个特殊的构造函数来实现
    看看这个类:(转自八卦小子在 http://community.csdn.net/Expert/topic/3328/3328701.xml?temp=.9649469 的回帖)
    [Serializable]
    public class MyObject : ISerializable 
    {
      public int n1;
      public int n2;
      public String str;  public MyObject()
      {
      }  protected MyObject(SerializationInfo info, StreamingContext context)
      {
        n1 = info.GetInt32("i");
        n2 = info.GetInt32("j");
        str = info.GetString("k");
      }  public virtual void GetObjectData(SerializationInfo info, 
    StreamingContext context)
      {
        info.AddValue("i", n1);
        info.AddValue("j", n2);
        info.AddValue("k", str);
      }
    }