自訂類別 繼承System.Web.UI.Page 且 Override Page_load 或是 onload 讓你的網頁更加有彈性

資料來源:How to Execute Page_Load() in Page’s Base Class?

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);
    }
}

如果在加上自已的一些function,像是是否已經登入、載入cookie值、資料庫連線設定、自訂資料庫取得資料的自訂Function、判斷是否有取的特定get的值,再做一些指定的工作。將一些必要的函式功能通通可以寫在這裏,就不用在每個Page各寫一次,造成維護上的麻煩。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s