我在aspx页面放了一个TextBox和一个Button<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    </form>
</body>然后在aspx.cs文件中写事件 protected void Page_Load(object sender, EventArgs e)
        {
        }        protected override void LoadViewState(object savedState)
        {
            Response.Write(" this is LoadViewState event!");
            base.LoadViewState(savedState);
        }        protected override object SaveViewState()
        {
            Response.Write(" this is SaveViewState event!");
            return base.SaveViewState();
        }
为什么无论我怎么更改TextBox的值后点Button,LoadViewState事件都不执行呢?

解决方案 »

  1.   

    SaveViewState 事件每次都有执行的。
    但LoadViewState事件每次都不执行,请问大家这是什么原因呢?
      

  2.   

    TextBox 并不是在 LoadViewState 的时候加载数据的,
    TextBox 实现了IPostBackDataHandler 接口,
    它的数据加载 是在 LoadPostData 时  
    也就是在 LoadViewState 后, Page_load 前
    base.LoadViewState(savedState);这个方法只会加载Page本身的视图状态,并不会加载页面上子控件的视图状态,所以在这个方法调用的前后,子控件的视图状态并不会变化。
      

  3.   

    http://topic.csdn.net/u/20081207/17/07fdf25e-b2f1-4161-b35e-832590f12d89.html、
    之前有人问过
      

  4.   


    LoadPostData 事件也不触发哦..
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server" Text="123"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </div>
        </form>
    </body>public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
               
            }        protected override void LoadViewState(object savedState)
            {
                base.LoadViewState(savedState);
            }        public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
            {
                return true;
            }
        }
    LoadViewState和LoadPostData事件都不触发..
      

  5.   

    控件首先要判断自己的ViewState有没有值,如果有才加载,然后它直接递归调用子控件。如果你在Page中没有使用到ViewState,而只是子控件使用了ViewState,那么只有子控件才会调用它,而Page不会。
      

  6.   

    其实我只是想在PostBack时获取到TextBox的旧值与新值.我并没有关闭TextBox的EnableViewState.
    这样不是Page页自动维护TextBox的ViewState吗?  
    为什么在PostBack时不触发LoadViewState事件呢?
      

  7.   


    Page维护Page的ViewState,TextBox维护TextBox的ViewState,各不相干。
      

  8.   


    呵呵,我在Page_Load事件中添加了一个ViewState值..
     protected void Page_Load(object sender, EventArgs e)
            {
                ViewState["aa"] = "abc";
            }LoadViewState是有执行了..
    但一定要这样吗?LoadViewStaet不是每次在回发时都会执行的吗?
      

  9.   

    看一看看源代码,在类System.Web.UI.Control中定义:internal void LoadViewStateRecursive(object savedState)
    {
        if ((savedState != null) && !this.flags[4])
        {
            if ((this.Page != null) && this.Page.IsPostBack)
            {
                object first = null;
                object state = null;
                ArrayList childState = null;
                Pair pair = savedState as Pair;
                if (pair != null)
                {
                    first = pair.First;
                    childState = (ArrayList) pair.Second;
                }
                else
                {
                    Triplet triplet = (Triplet) savedState;
                    first = triplet.First;
                    state = triplet.Second;
                    childState = (ArrayList) triplet.Third;
                }
                try
                {
                    if ((state != null) && (this._adapter != null))
                    {
                        this._adapter.LoadAdapterViewState(state);
                    }
                    if (first != null)
                    {
                        this.LoadViewState(first);
                    }
                    if (childState != null)
                    {
                        if (this.LoadViewStateByID)
                        {
                            this.LoadChildViewStateByID(childState);
                        }
                        else
                        {
                            this.LoadChildViewStateByIndex(childState);
                        }
                    }
                }
                catch (InvalidCastException)
                {
                    throw new HttpException(SR.GetString("Controls_Cant_Change_Between_Posts"));
                }
                catch (IndexOutOfRangeException)
                {
                    throw new HttpException(SR.GetString("Controls_Cant_Change_Between_Posts"));
                }
            }
            this._controlState = ControlState.ViewStateLoaded;
        }
    }可以看到,有判断“if (first != null)”。其实这种知识,自己看.net framework源代码就行了,上csdn不一定能问出什么。
      

  10.   

    而 Page 是从 TemplateControl 类中继承的这个方法,后者是从 Control 中继承的这个方法。