C# 该名称在当前上下文中不存在。不上课
我对C代码非常陌生,所以这可能非常简单。在我的Site.Master.cs中,我有以下代码:C# 该名称在当前上下文中不存在。不上课,c#,C#,我对C代码非常陌生,所以这可能非常简单。在我的Site.Master.cs中,我有以下代码: GetLoggedInUserProperties(); lblLoginUser.Text = string.Format("Welcome {0}", Session[SessionVars.UserName]); 在类文件中,我将以下内容放在公共类中: void GetLoggedInUserProperties() { string sLoginId = Program
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);
}
类文件中没有错误,但my Site.Master.cs在类文件中找不到代码
我相信有更好的方法可以做到这一点,所以请随时让我知道。此外,lblLoginUser似乎也不起作用。它也有同样的错误。我尝试过重新创建标签并删除从未返回的设计器文件。不确定这是否相关
谢谢。您需要公开或保护您的方法,以便在标记中可见。因此,如果您想从标记中引用GetLoggedInUserProperties,您需要将方法声明更改为如下内容
protected void GetLoggedInUserProperties()
{
}
公开你的方法 这是你的课
public class ClassName
{
public void GetLoggedInUserProperties()
{
}
}
public MainWindow()
{
InitializeComponent();
ClassName instance = new ClassName();
instance.GetLoggedInUserProperties();
}
或者将类设置为静态并调用方法:
public static class ClassName
{
public static void GetLoggedInUserProperties()
{
}
}
public MainWindow()
{
InitializeComponent();
ClassName.GetLoggedInUserProperties();
}
最简单的方法是将该方法设置为静态,然后使用ClassName.GetLoggedInUserProperties调用它,其中ClassName是类的名称。我不完全理解。是否将GetLoggedInUserProperties作为公共类中的方法?如果它是公共静态的,那么您可以通过ClassName.GetLoggedInUserProperties调用该方法。我使用了public,这很有效。没有。我发誓我以前试过公开,但这次成功了。谢谢