[英]access static variable from aspx page
public partial class Page1 :System.Web.UI.Page
{
public static LGDP.LDGPSession var1 = null;
private void Login(this, EventArgs e)
{
var1 = new LGDPSession(this, data, data);
}
public bool IsLoggedIn()
{
bool login = false;
if (var1 != null)
{
login = true;
}
return var1;
}
}
How do I access the Page1 static var1 or function IsLoggedIn() from Page2.apsx ? 如何从Page2.apsx访问Page1 static var1或函数IsLoggedIn()?
public partial class Page2 :System.Web.UI.Page
{
Page1.(nothing shows up here)
}
ANSWER ----- created separate class and accessed public var in pageload / postback 答案-----在pageload / postback中创建了单独的类并访问了public var
private static bool _login = false;
public static void SetLoggedIn(object lgdps)
{
if (lgdps == null)
{
_login = false;
}
if (lgdps != null)
{
_login = true;
}
}
public static bool IsLogin
{
get { return _login; }
}
Your function IsLoggedIn
in Page1
doesn't compile. 您的Page1
中的函数IsLoggedIn
无法编译。 It has to return something: 它必须返回一些东西:
public bool IsLoggedIn()
{
bool login = false;
if (var1 != null)
{
login = true;
}
return login;
}
Or simply: 或者干脆:
public bool IsLoggedIn()
{
return var1 != null;
}
Once the page compiles, its members should show up in the intellisense. 页面编译后,其成员应显示在intellisense中。
It's better to create a base class with your functions in it: 最好用你的函数创建一个基类:
public class BasePage : Page
{
public bool IsLoggedIn()
{
bool login = false;
if (var1 != null)
{
login = true;
}
}
}
And then you can access IsLoggedIn
from you pages when you inherit from BasePage
然后,当您从BasePage
继承时,可以从您的页面访问IsLoggedIn
public partial class Page1 : BasePage
{
}
public partial class Page2 : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if(IsLoggedIn())
{
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.