0

我有一个应用程序,我试图在会话超时时重定向,因此在我的母版页中,我正在检查重定向的会话变量是否为空,但问题是我有来自母版页和派生的 Page_Load 的其他页面(派生)页面我也在那里引用了一些会话变量,我观察到派生页面(内容)的 PAGE_LOAD 事件首先触发,母版页 PAGE_LOAD 稍后触发,因此错误弹出“未设置对象引用”。

顺便说一句,我正在 LOGIN_BUTTON_PRESSED EVENT 上编写以下代码。

FormsAuthenticationTicket ticket - new FormsAuthenticationTicket(1, userName, DateTime.Now, DataTime.Now.AddMinutes(20), true, myRoles, FormsAuthentication.FormsCookiePath);

Session["uid"] = userName.Text;  
Session["ufullname"] = ufname;  

string hashCookies = FormsAuthentication.Encrypt(ticket);  

HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);    

Response.Cookies.Add(cookie);  

Response.Redirect("~/Main.aspx");

请通过示例提出解决此问题的解决方案。

4

1 回答 1

0

一种选择是将母版页 Page_Load 事件中的代码移动到 Page_Init 事件:

protected void Page_Init( object sender, EventArgs e )
{
    //Your code goes here
}

请参阅ASP.NET 页面生命周期概述

于 2010-08-03T07:07:18.110 回答