Is it possible to persist the viewstate between pages in ASP.NET?

老子叫甜甜 提交于 2019-12-07 07:09:58

问题


I have a button (view state enabled) in Master web page and set it to visible=false in one of the child web pages. If a second child page is opened, the button state (visible=false) is not persisting.

It seems viewstate is only valid for one page and is not transferred to other web pages. Is there some kind of trick to make viewstate global for all web pages?


回答1:


No, viewstate is page specific. You will need to use something like a session variable or a querystring parameter to pass your state between pages.




回答2:


No, You cannot make view state global, they are page specific. I would suggest to use cookies if you really want to make it client side otherwise you can use session.




回答3:


If you need to store on a "global" level, you should be using the Application State. You could also use Cache Object. You may be wanting to pass values from one page to another, you can achieve this by using the Context object in combination with the Server.Transfer.

1) You need a public property on the source page returning the Value to pass

namespace SomeNameSpace
{
    public partial class SourcePage: System.Web.UI.Page
    {
        public string ValueToPass
        {
            get
            {
                if (Context.Items["ValueToPass"] == null)
                    Context.Items["ValueToPass"] = string.Empty;
                return (string)Context.Items["ValueToPass"];
            }
            set
            {
                Context.Items["ValueToPass"] = value;
            }
        }
        ........
    }
}

2) Do a Server.Transfer(DestinationPage.aspx) 3) In the Page_Load event of the destination page

namespace SomeNameSpace
{
    public partial class SourcePage: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var value = this.Context.Items["ValueToPass"];
        }
    }
}

Hope this helps



来源:https://stackoverflow.com/questions/5577591/is-it-possible-to-persist-the-viewstate-between-pages-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!