开发者

Are control attributes stored in ViewState too?

I am currently reading a book about ASP.NET and I am a bit confused about one concept.

All the time I thought that no value can be saved over a postback if it is not either stored in viewstate, session state and so on. However, now I read about using Panels to have multi view content in one Page. I have the following code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;
    }
}

protected void GoNext_Click(object sender, EventArgs e)
{
    if (Panel1.Visible)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
    }
    else if (Panel2.Visible)
    {
        Panel2.Visible = false;
        Panel3.Visible = true;
    }
    else if (Panel3.Visible)
    {
        Panel3.Visible = false;
        Panel1.Visible = true;
    }
}

Confusing part about that code is that, when I have Panel3 visible, for example, then how does ASP.NET know to also hide Panel1? (Because In the previous 开发者_运维百科else if statement, I only tell ASP.NET to hide Panel2 and show Panel3, but I tell nothing about Panel1).

Are these values stored in ViewState?


I believe the information is still stored in ViewState on each postback, even though the control doesn't get rendered to the page. When the control gets rendered again, the information from ViewState is loaded to the control.


Yes - it has to be stored in the viewstate. Otherwise the control state would reset upon postback (which it doesn't).

As some added info - This is one of the 'gotchas with pros and cons" with web forms when you forget to show/hide some items and the user navigates back to the page freshly and the state is now different than the last time they saw it. MVC helps avoid this with the POST/Redirect/GET pattern which can technically be done with web forms as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜