简体   繁体   中英

Access Parent Page ViewState from the user control?

有没有办法从用户控件访问父页面ViewState。

Yes you can ... To do so you just need to follow a basic trick ..

First inherit the caller page by a base page (using a base page over the project always a good practice it help later very much) like below ...

public abstract class BasePage : Page
{
  public StateBag ViewState
  {             
     get
       {
          return base.ViewState;
       }
  }
}

Later you can call this property from usercontrol ........

var CallerPage = this.Page as BasePage;
if (CallerPage!=null)
{
  var CallerPageViewState = CallerPage.ViewState;
  //Do your rest job
}

The ViewState property of the Page class is protected . Therefore, there's no way to access it from a user control, unless you're willing to break encapsulation by using reflection .

In a word no. Depending on what you want to do though there may be another way to do what you're trying to achieve

More information on what you're wanting to achieve will help people to answer your question more fully

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM