Search code examples
c#asp.netmaster-pages

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


I need to grab the value of a hidden field from a Master page and use the value as a global variable on a child page.

What I did was put this into the Public Partial 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# doesn't like this, says "A field initializer cannot reference the non-static field, method, or property 'Control.Page'"

How would I go about doing something like this?


Solution

  • This is not a "global" variable, but a class member. See if making it a property works for you:

    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;
      }
    }