【问题标题】:Getting the value from a hiddenfield on a Master page into a global variable on a Child page从母版页上的隐藏字段中获取值到子页上的全局变量中
【发布时间】:2017-01-11 07:04:57
【问题描述】:

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

我所做的就是把它放到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# 不喜欢这样,说“A field initializer cannot reference the non-static field, method, or property 'Control.Page'”

我将如何去做这样的事情?

【问题讨论】:

    标签: c# asp.net master-pages


    【解决方案1】:

    这不是一个“全局”变量,而是一个类成员。看看让它成为一个属性是否适合你:

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

    【讨论】:

    • 我把它放在我的页面上,它似乎接受它,但是当我尝试在 If 语句中使用它时(即 if (SecLevel = "")),它会在 SecLevel 下划线并显示错误状态:“不能将属性或索引器‘frmBenefitSummaryList.SecLevel’分配给——它是只读的”。我不是要给它赋值,而是要检查它的值。
    • if (SecLevel == "")) 或 if (string.IsNullOrEmpty(SecLevel))
    • 完美!谢谢!
    猜你喜欢
    • 2013-03-19
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多