开发者

System.Web.Ui.Page derived class that can have page events extended?

I have created a BasePage class that inherits from System.Web.Ui.Page. In that base class I have a bool property that checks to see if a page is secure or not. Initially, I put the code in the PreInit event (of the base class), but after thinking about it, my derived pages will not be able to set the bool value before PreInit. I then thought of setting the value in PreInit of the dervcied pages and checking that value in PageInit of the base class, but what if I need to use the PreInit in the derived page?

I thought about using partial methods, but I don’t think I can do that because the page events are not partials in System.Web.Ui.Page, right?

My BasePage class is an abstract class, by the way.

This is what I have now (I 开发者_运维知识库have not tested this, but assumed it may work):

public abstract partial class BasePage: System.Web.UI.Page
{
   public bool IsSecure { get; set; }

   protected void Page_Init(object sender, EventArgs e)
        {
            if (!IsSecure) return;
            if (PageMaster == null)
                return;
            if (!PageMaster.IsUserLoggedIn)
            {
                HttpContext.Current.Response.Redirect("~/WebForms/LogIn.aspx");
            }
        }  
}


public partial class _Default : BasePage
{
   protected void Page_PreInit(object sender, EventArgs e)
   {
     IsSecure = true;
   }

}


A better solution may be to override the OnInit method in your base class. You can now still handle the init event in your pages, with the secure check carried out before the event is raised.

so:

public abstract partial class BasePage: System.Web.UI.Page
{
    public bool IsSecure { get; set; }

    protected override void OnInit(EventArgs e)
    {
        if (!IsSecure) return;
        if (PageMaster == null)
            return;
        if (!PageMaster.IsUserLoggedIn)
        {
            HttpContext.Current.Response.Redirect("~/WebForms/LogIn.aspx");
        }

        base.OnInit(e)
    }  
}

public partial class _Default : BasePage
{   
   protected void Page_PreInit(object sender, EventArgs e)
   {
      IsSecure = true;
   }

}


I suggest you make the IsSecure property abstract (and read-only) and have your derived pages implement it. The logic that determines the value of the property is contained in the getter of the property.

In BasePage:

protected abstract bool IsSecure { get; }

In _Default etc:

protected override bool IsSecure 
{
    get { // return true or false depending on some condition }
}


Why not set your IsSecure in the Constructor, its as early as you're going to get ?

Just define it like :

public _Default()
{
 IsSecure = true;
}


Your code is fine, if you want to be able to extend the PageInit, just override and call the base in the derived class.

public partial class _Default : BasePage
{
    protected override void Page_Init(object sender, EventArgs e)
    {
        base.Page_Init(sender, e);
        //more code here
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜