Can I save view state between pages in ASP.NET?

I have a button (view state enabled) on the main web page and set it to visible=false on one of the child web pages. If the second child page opens, the state of the button ( visible=false) not saved.

The viewstate seems to be valid for only one page and does not migrate to other web pages. Is there any trick to making viewstate global for all web pages?

+4
source share
3 answers

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

+13
source

No, you cannot pretend to be state global, they are page specific. I would suggest using cookies if you really want to do this on the client side, otherwise you can use a session.

+3
source

If you need to save at the "global" level, you must use the state of the application. You can also use the Cache object. You might want to pass values ​​from one page to another, you can achieve this using the Context object in conjunction with Server.Transfer.

1) You need a public property on the original page that returns 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) Make Server.Transfer (DestinationPage.aspx) 3) In the Page_Load event of the landing 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

0
source

Source: https://habr.com/ru/post/1347052/


All Articles