[英]The name does not exist in the current context. Not seeing class
I am very new to C# coding, so this may be very simple. 我对C#编码非常陌生,因此这可能非常简单。 In my Site.Master.cs, I have the following code:
在我的Site.Master.cs中,我有以下代码:
GetLoggedInUserProperties();
lblLoginUser.Text = string.Format("Welcome {0}", Session[SessionVars.UserName]);
In a class file, I have put the following in a Public Class: 在一个类文件中,我将以下内容放入了公共类:
void GetLoggedInUserProperties()
{
string sLoginId = Program.ExtractUserName(HttpContext.Current.Request.ServerVariables["LOGON_USER"]);
HttpContext.Current.Session[SessionVars.LoginId] = sLoginId;
VerifyInAD(sLoginId);
}
There are no errors in the class file, but my Site.Master.cs cannot find the code in the class file. 类文件中没有错误,但是我的Site.Master.cs无法在类文件中找到代码。
I am sure there is a better way to do this, so feel free to let me know. 我相信有更好的方法可以做到这一点,请随时告诉我。 Also, the lblLoginUser does not seem to work either.
另外,lblLoginUser似乎也不起作用。 It has the same error.
它具有相同的错误。 I have tried recreating the label and deleting the designer file (which never came back).
我尝试过重新创建标签并删除设计器文件(从未恢复过)。 Not sure if this is related or not.
不知道这是否相关。
Thank you. 谢谢。
You need to make your methods public
or protected
to be visible from the markup. 您需要
public
或protected
您的方法,以使其从标记中可见。 So if you want to reference GetLoggedInUserProperties()
from the markup, you'll need to change your method declaration to something like this. 因此,如果您想从标记中引用
GetLoggedInUserProperties()
,则需要将方法声明更改为如下所示。
protected void GetLoggedInUserProperties()
{
}
Make your method public 公开您的方法
Here your class 这是你的课
public class ClassName
{
public void GetLoggedInUserProperties()
{
}
}
public MainWindow()
{
InitializeComponent();
ClassName instance = new ClassName();
instance.GetLoggedInUserProperties();
}
Or make your class static
and call your method : 或者将您的类
static
并调用您的方法:
public static class ClassName
{
public static void GetLoggedInUserProperties()
{
}
}
public MainWindow()
{
InitializeComponent();
ClassName.GetLoggedInUserProperties();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.