改變網頁時,我有一個母版頁設置UPS的一些變量,我想在整個網站使用..爲什麼我會得到「對象引用未設置爲對象的實例」。在asp.net
protected void Page_Load(object sender, EventArgs e)
{
//Get users name from AD
str_DomainName = HttpContext.Current.User.Identity.Name;
str_CurrentLogin = str_DomainName.Substring(5);
//Display current user information
DirectorySearcher search = new DirectorySearcher("LDAP://DCHS");
search.Filter = String.Format("(SAMAccountName={0})", str_CurrentLogin);
SearchResult result = search.FindOne();
DirectoryEntry entry = result.GetDirectoryEntry();
lbl_CurrentUser.Text = result.Properties["givenName"][0].ToString() + ' ' + result.Properties["sn"][0].ToString();
// Get SID
IntPtr logonToken = WindowsIdentity.GetCurrent().Token;
WindowsIdentity windowsId = new WindowsIdentity(logonToken);
//Set session variabls
this.CurrentFirstName = result.Properties["givenName"][0].ToString();
//this.CurrentEmail = result.Properties["mail"][0].ToString();
//this.CurrentSID = windowsId.User.ToString();
//this.CurrentUserName = str_CurrentLogin;
//this.CurrentFullName = lbl_CurrentUser.Text;
//this.CurrentDomain = str_DomainName;
this.Session.Add("currentEmail", result.Properties["mail"][0].ToString());
}
public String CurrentFirstName
{
get { return (String)ViewState["currentFirstName"]; }
set { ViewState["currentFirstName"] = value; }
}
然後我打電話給他們在我的defalut.aspx頁面如下:
protected void Page_PreRender(object sender, EventArgs e)
{
//try
//{
lbl_FullName.Text = Master.CurrentFullName;
lbl_SID.Text = Master.CurrentSID;
testLabel.Text = Master.CurrentEmail;
//}
//catch (Exception ex)
//{ }
}
這工作得很好。如果我然而從默認的頁面導航離去,然後我得到下面的錯誤..
Object reference not set to an instance of an object.
其中lbl_FullName.Text = Master.CurrentFullName;
線
如果我取消了嘗試捕捉然後正常工作,但我不認爲這是避免故障的正確的方法..
我只是新ASP,所以是很好..
編輯:
變量被設置在Master.cs如下。
public String CurrentUserName
{
get { return (String)ViewState["currentUserName"]; }
set { ViewState["currentUserName"] = value; }
}
它可能取決於處理各種事件的順序(也就是說,您可能試圖在頁面設置在母版頁中之前從頁面讀取值)。看看[this](http://msdn.microsoft.com/en-us/library/dct97kc3.aspx)並嘗試檢查事件序列 – 2011-03-18 00:50:19
什麼是Master變量?它會在其他頁面上設置還是僅在默認情況下設置? – Spikolynn 2011-03-18 00:52:33
請參閱我的編輯,瞭解我如何生成變量 – Matt 2011-03-18 01:22:22