从母版页变量总是空 [英] Variables from Master Page always empty
问题描述
我有这些变量在我的母版设置
I have these variables set on my MasterPage
public string CurrentUser = "";
public string UserDomain = "";
protected void Page_Load(object sender, EventArgs e)
{
string Name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string[] userDetails = Request.LogonUserIdentity.Name.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
UserDomain = userDetails[0];
CurrentUser = userDetails[1];
}
在一个子页面,我只是想读这些值回:
In a child page I am simply trying to read those values back:
CurrentUser = ((SiteMaster)Page.Master).CurrentUser;
UserDomain = ((SiteMaster)Page.Master).UserDomain;
他们总是回来了空。
我可以看到,他们设置正确的母版页上,但他们迷失在路上的内容页面。
They are always coming back empty. I can see that they are set correctly on the Master page but they get lost on the way to the content page.
我在做什么错了?
推荐答案
母版页Load事件涉及内容页Load事件之后。这可能意味着,变量初始化得到的之后的他们在您的内容页面访问。
The Master page Load event comes after the Content page Load event. That probably means that the variables get initialized after they are accessed in your content page.
您可以选择设置在母版的初始化
事件时,这些属性,或对它们进行初始化的访问属性。
You could opt to set those properties in the Init
event of the masterpage, or initialize them on accessing a property.
请参阅在ASP.NET主事件页和内容页的。
这篇关于从母版页变量总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!