2014-03-04 82 views
0

在用户控制页面Page_preinit事件没有触发。 给定的代码波纹管:为什么UserControl Page_PreInit事件不会触发?

protected void Page_PreInit(object Sender, EventArgs e) 
{ 
    if (!Page.User.Identity.IsAuthenticated && !Page.User.IsInRole("Admin")) 
    { 
     Response.Redirect("abcd/Index.aspx?Auth=Fail"); 
    } 
    else 
    { 
     FormsIdentity id = (FormsIdentity)Page.User.Identity; 
     FormsAuthenticationTicket ticket = id.Ticket; 
     String[] userDatas = ticket.UserData.Split('|'); 
     ViewState["Role"] = userDatas[0]; 
     ViewState["Language"] = userDatas[2]; 
     this.Page.Theme = userDatas[1]; 
    } 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1)); 
    Response.Cache.SetNoStore(); 
} 

任何想法?

+0

把你的设计页面也放在 –

+2

你的用户控件上的'AutoEventWireup'属性是如何设置的?在你的网页上? –

+0

不知道为什么这个问题是downvoted。 PreInit没有被用户控制解雇,特别是如果你花大部分时间在浏览页面,这一点并不明显。 – mac9416

回答

4

UserControls实际上是从Control类派生的,并且没有任何PreInit事件可用。

继承层次

System.Object 
    System.Web.UI.Control 
    System.Web.UI.TemplateControl 
     System.Web.UI.UserControl 

PreInit事件是供页面类。

相关问题