1

I have functionality I reuse in every web form. So I want this reuse in a base class and have the web form inherit the base class. Does my example use correct Object oriented practice ?? Here is an example:

using System;
using System.Web;
namespace Template1
{

  public abstract class AllPageBaseClass : System.Web.UI.Page
  {
    public AllPageBaseClass()
    {
      this.Load += new EventHandler(this.Page_Load);
    }

        protected void Page_Load(object sender, EventArgs e)
         {
              if (Session["stuff"] == null)
                  Response.Write("Session Is Empty");
              // More error checking common to all pages here
         }
     }
 }


using System.Lots_Of_Stuff;

// Do I need System; and System.Web; here ??

namespace Template1
{
    public partial class Home : AllPageBaseClass
    {
        protected new void Page_Load(object sender, EventArgs e)
        {
        // All unique Page_load stuff here
        }
        ....
    }
}
4

1 回答 1

1

您不需要订阅 Load 事件。

我的一个项目示例:

public class SecuredPage:System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (...) 
        {
            // do something
        }
    }
}

您的内容页面应如下所示:

 public partial class Home : AllPageBaseClass
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          // All unique Page_load stuff here
        }
        ....
    }

您还可以查看新运算符的用途。

于 2013-01-13T19:26:27.863 回答