private int nValue
{
     get
     {
          if(ViewState["nValue"]!=null)
            return (int)ViewState["nValue"];
          else
            return 0;
     }
     set
     {
          ViewState["nValue"] = value;
     }
}

解决方案 »

  1.   

    PageInit()事件在LoadPageViewState()之前
      

  2.   

    public int id;
    protected override void OnPreRender(System.EventArgs e)
    {
    ViewState["nValue"]=id;
    }
      

  3.   

    不能在Page_Init里初始化ViewState
      

  4.   

    1。 在Page_Init里,ViewState还没初始化System.Diagnostics.Debug.Assert(ViewState["AnyKey"] == null);2。你可以试着为某个key初始化ViewState,譬如,ViewState["key"] = DateTime.Now.ToString();但它的值会被PostBack后的LoadViewState冲掉
    <script language="C#" runat="server">
    void Page_Init(Object o, EventArgs e)
    {
      Response.Write("In Page_Init: ViewState is "+ (ViewState["abc"] == null? "emtpy":ViewState["abc"].ToString()) + "<BR>");
      Response.Write("In Page_Init: PostBack?" + Page.IsPostBack.ToString() + "<BR>");
      
      ViewState["abc"] = DateTime.Now.ToString();  Response.Write("In Page_Init:"+ ViewState["abc"].ToString() + "<BR>");
    }void Page_Load(Object o, EventArgs e)
    {
      if (!IsPostBack)
       ViewState["abc"] = DateTime.Now.ToString();
      
      Response.Write("In Page_Load:"+ ViewState["abc"].ToString() + "<BR>");
    }
    </script>
    <form runat="server">
     <asp:Button runat="server" Text="Refresh"/>
    </form>
      

  5.   

    参考Understanding ASP.NET View State
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.aspViewState: All You Wanted to Know
    http://authors.aspalliance.com/PaulWilson/Articles/?id=7Page Events: Order and PostBack
    http://authors.aspalliance.com/PaulWilson/Articles/?id=6
      

  6.   

    saucer(思归/MVP)
     我想你没有看清楚我得程序
     我是在Page_Init()用!Page.IsPostBack来初始化ViewState得,
     我就是想在PostBack得时候能够LoadViewState把Request时候初始值load回来
     而实际上不行,它会说该ViewState未空引用,
     而我在Page_Load得时候用!Page.IsPostBack就可以打印出初始值,
     但postback时这个值不见了。而如果我在Page_Load里面初始化则可以
     所以我想问得是为什么在Init事件里面不能对ViewState初始化,
     注意是初始化(使用!Page.IsPostBack来进行),
     
     那么我想在Request得时候应该不会调用LoadViewState把,因为还没执行过SaveViewState
     那么我认为在Request(!Page.IsPostBack)得时候,在哪里对ViewState进行初始化都是一样,
     只要保证在执行SaveViewState之前。 这是我的理解,不知道正确否,还是微软对ViewState得初始化有规定,
     而它得规定是什么原因?我想我上面得理解没什么不合理得地方 其实我是知道在Init事件里面初始化不行,但是我想知道为什么??
      

  7.   

    请仔细读一下Understanding ASP.NET View State
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp
    你可以在任何时候给ViewState赋值,但只有在调用TrackViewState()之后的改变才会在SaveViewState时写入__VIEWSTATE从上面这篇文章,The TrackViewState() method is called at the end of the initialization stage,
     which happens after the instantiation stage. 
      

  8.   

    在page_init 初始化viewstate是肯定会出错未将对象引用到实例的,你可以在page_load来进行初始化
      

  9.   

    saucer(思归/MVP)
    首先很感谢;
    不过我还有个问题:
    对于页面的ViewState确实如此,但是为什么对于页面控件的ViewState却可以在Init里面进行初始化呢for example
    these codes work well
    ///////////////////////////////
    protected System.Web.UI.WebControls.DropDownList ddlPrice;
    private void Page_Init()
    {
          if(!Page.IsPostBack)
          {
               ddlPrice.Items.Add("无价之宝");
               ddlPrice.Items.Add("价值连城");
           }
    }
    对于dllPrice的Items确实是用ViewState保存了下来
    为什么?
      

  10.   

    viewstate是用来保存控件的视图状态的即记录其的信息以在逻辑代码中引用,但只能在页面的prerend事件进存储及在load事件中进行引用
      

  11.   

    楼上你说得也太夸张了把,根本对ViewState不了解啊
      

  12.   

    ViewState是在Page类的LoadViewState方法执行完后才可用,Page的Init执行是LoadViewState方法还未执行。
      

  13.   

    saucer(思归/MVP)
    首先很感谢;
    不过我还有个问题:
    对于页面的ViewState确实如此,但是为什么对于页面控件的ViewState却可以在Init里面进行初始化呢for example
    these codes work well
    ///////////////////////////////
    protected System.Web.UI.WebControls.DropDownList ddlPrice;
    private void Page_Init()
    {
          if(!Page.IsPostBack)
          {
               ddlPrice.Items.Add("无价之宝");
               ddlPrice.Items.Add("价值连城");
           }
    }
    对于dllPrice的Items确实是用ViewState保存了下来
    为什么?
      

  14.   

    Page_Init中进行属性nValue 赋值时,引用Page.ViewState,而此时ViewState还为空(未加载),如果在Page_Load中赋值就不会出现错误了
      

  15.   

    you don't need to do it in a UserContorl, the same thing happens in Page's Page_Init, DropDownList calls TrackViewState() in the Items.Add method
      

  16.   

    and then why not call the TrackViewState() automatically at the beginning of the page life cycle,and then everywhere can use viewstate.
    that's simple and clearly. why define to call trackviewstate() after init event?
      

  17.   

    because the code creates those declared objects in the page in the initialization stage, and you don't want to save those information in the viewstate