该名称在当前上下文中不存在。没看到上课

时间:2014-06-24 13:18:30

标签: c#

我对C#编码很新,所以这可能非常简单。在我的Site.Master.cs中,我有以下代码:

GetLoggedInUserProperties();
lblLoginUser.Text = string.Format("Welcome {0}", Session[SessionVars.UserName]);

在类文件中,我将以下内容放在公共类中:

void GetLoggedInUserProperties()
    {
        string sLoginId = Program.ExtractUserName(HttpContext.Current.Request.ServerVariables["LOGON_USER"]);
        HttpContext.Current.Session[SessionVars.LoginId] = sLoginId;

        VerifyInAD(sLoginId);
    }

类文件中没有错误,但是我的Site.Master.cs找不到类文件中的代码。

我相信有更好的方法可以做到这一点,所以请随时告诉我。此外,lblLoginUser似乎也不起作用。它有同样的错误。我尝试重新创建标签并删除设计器文件(从未返回)。不确定这是否相关。

谢谢。

2 个答案:

答案 0 :(得分:0)

您需要从标记中看到方法publicprotected。因此,如果您想从标记中引用GetLoggedInUserProperties(),则需要将方法声明更改为类似的内容。

protected void GetLoggedInUserProperties()
{
}

答案 1 :(得分:0)

让您的方法公开

这是你的班级

public class ClassName
 {
       public void GetLoggedInUserProperties()
       {

       }

 }

public MainWindow()
{
                InitializeComponent();
                ClassName instance = new ClassName();
                instance.GetLoggedInUserProperties();

}

或者上课static并致电您的方法:

  public static class ClassName
   {
       public static void GetLoggedInUserProperties()
       {

       }

    }



 public MainWindow()
  {
                InitializeComponent();
                ClassName.GetLoggedInUserProperties();

   }