从母版页上的隐藏字段中获取值到子页上的全局变量

Getting the value from a hiddenfield on a Master page into a global variable on a Child page

我需要从母版页中获取隐藏字段的值,并将该值用作子页上的全局变量。

我所做的是将其放入 Public 部分 Class:

public partial class frmBenefitSummaryList : System.Web.UI.Page
{
    //public string PlanID = Convert.ToString(Request.QueryString["PlanID"]);
    //public string AuditID = Convert.ToString(Request.QueryString["AuditID"]);
    //public string UID = LoginSecurity.GetUser("ID");
    public string SecLevel = (HiddenField)Page.Master.FindControl("hdnSecLevel");

C# 不喜欢这样,说“A field initializer cannot reference the non-static field, method, or property 'Control.Page'

我将如何做这样的事情?

这不是 "global" 变量,而是 class 成员。看看将其设置为 属性 是否适合您:

private HiddenField secLevelField = null;
public string SecLevel {
  get
  {
    if (secLevelField == null)
      secLevelField = (HiddenField)Page.Master.FindControl("hdnSecLevel");
    if (secLevelField != null)
      return secLevelField.Value;
    else
      return null;
  }
}