代码之家  ›  专栏  ›  技术社区  ›  DaveDev

如何在页的基类中执行page_load()?

  •  30
  • DaveDev  · 技术社区  · 14 年前

    我有以下PerformanceFactSheet.aspx.cs页类

    public partial class PerformanceFactsheet : FactsheetBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // do stuff with the data extracted in FactsheetBase
            divPerformance.Controls.Add(this.Data);
        }
    }
    

    其中factsheetbase定义为

    public class FactsheetBase : System.Web.UI.Page
    {
        public MyPageData Data { get; set; } 
        protected void Page_Load(object sender, EventArgs e)
        {
            // get data that's common to all implementors of FactsheetBase
            // and store the values in FactsheetBase's properties
            this.Data = ExtractPageData(Request.QueryString["data"]);            
        }
    }
    

    问题是factsheetbase的页面加载没有执行。

    有人能告诉我我做错了什么吗?有没有更好的方法得到我想要的结果?

    谢谢

    6 回复  |  直到 6 年前
        1
  •  49
  •   this. __curious_geek    14 年前

    我们也面临同样的问题,你只要 在构造函数中注册处理程序。:)

    public class FactsheetBase : System.Web.UI.Page 
    { 
    
        public FactsheetBase()
        {
            this.Load += new EventHandler(this.Page_Load);
        }
    
        public MyPageData Data { get; set; }  
        protected void Page_Load(object sender, EventArgs e) 
        { 
            // get data that's common to all implementors of FactsheetBase 
            // and store the values in FactsheetBase's properties 
            this.Data = ExtractPageData(Request.QueryString["data"]);             
        } 
    }
    

    另一种方法是 覆盖加载() 不太受欢迎。

    public class FactsheetBase : System.Web.UI.Page 
    { 
    
        public FactsheetBase()
        {
        }
    
        public MyPageData Data { get; set; }  
        protected override void OnLoad(EventArgs e)
        {
            //your code
            // get data that's common to all implementors of FactsheetBase 
            // and store the values in FactsheetBase's properties 
            this.Data = ExtractPageData(Request.QueryString["data"]);             
    
            base.OnLoad(e);
        }
    }
    
        2
  •  7
  •   n8wrl    14 年前

    在PerformanceFactSheet中重写onload()并调用base.onload()而不是page-load()方法

        3
  •  4
  •   NeXuS    14 年前

    嗯,我可能错了,但我相信这是由于继承:您正在重写派生类中的factsheetbase page_load方法。

    为了执行它你应该做些

    public partial class PerformanceFactsheet : FactsheetBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load( sender, e );
            // do stuff with the data extracted in FactsheetBase
            divPerformance.Controls.Add(this.Data);
        }
    }
    

    编辑:n8wrl绝对给了你一个更清晰的解决方案(我不是aspx程序员)。

        4
  •  4
  •   serkanh    12 年前

    试试这个

     public partial class PerformanceFactsheet : FactsheetBase
    {
        public PerformanceFactsheet()
        {
            this.Load += new EventHandler(this.Page_Load);
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {            
            divPerformance.Controls.Add(this.Data);
        }
    }
    
    public abstract class FactsheetBase : System.Web.UI.Page
    {
        public MyPageData Data { get; set; }
        public FactsheetBase()
        {
            this.Load += new EventHandler(this.Page_Load);
        }
    
        new protected void Page_Load(object sender, EventArgs e)
        {            
            this.Data = ExtractPageData(Request.QueryString["data"]);
        }
    }
    
        5
  •  0
  •   kav    9 年前

    试试这个:

         public partial class PerformanceFactsheet : FactsheetBase
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
    base.Page_Load(sender, e);
            // do stuff with the data extracted in FactsheetBase
            divPerformance.Controls.Add(this.Data);
        }
    }
    
    public class FactsheetBase : System.Web.UI.Page
    {
        public MyPageData Data { get; set; } 
        protected virtual void Page_Load(object sender, EventArgs e)
        {
            // get data that's common to all implementors of FactsheetBase
            // and store the values in FactsheetBase's properties
            this.Data = ExtractPageData(Request.QueryString["data"]);            
        }
    }
    
        6
  •  0
  •   Tyler S. Loeper    6 年前

    将页面加载设置为公共,并从另一个页面以如下方式调用它:

    this.myPageOrUserControl.Page_Load(null, EventArgs.Empty);