2015-04-22 25 views
0

我定義了多個主,並在Form_Load事件上有一些共同的功能。現在我聲明一個派生自System.Web.UI.MasterPage的customMasterPage類,並將所有通用功能放入此中。但我想重寫onPage_Load()並想打印到Dervived類的控件中。我想從基類onLoad()覆蓋在c#中的派生類的控件(主頁從一個masterClass派生)

假設我有一個customMaster類

class customMasterClass:System:web.UI.MasterPage 
{ 
    override onLoad() 
    { 
     base(e) 

     //I want to use following child class controls(lblName) here to print 
     // lblName.text="" 
    } 
} 

class child1:customMaster 
{ 
    //Controls are.. 
    //Label:id=lblName; 

    Form_load() 
    { 
     lblName.text="test1 test1"; 
    } 
} 
class child2:customMaster 
{ 
    //Controls are.. 
    //Label:id=lblName; 

    Form_load() 
    { 
     lblName.text="test2 test2"; 
    } 
} 

如何做到這一點。請你給我建議提前

感謝

+0

在'的onPageLoad()'在派生頁面類,您可能需要將想要更改的字段傳遞給基類中的方法。 –

回答

0

基類不知道子類東西。話雖這麼說,你可以定義基類具有重寫方法派生類可用來設置特定的東西,比如:

public class BaseClass 
{ 
    public override OnLoad(object sender, EventArgs e) 
    { 
     base.OnLoad(e); 

     SetLabels();  //This will call the appropriate child class method 
    } 

    public virtual void SetLabels() 
    { 

    } 
} 

public class ChildClass1 : BaseClass 
{ 
    public override void SetLabels() 
    { 
     //Set the label here 
    } 
} 

public class ChildClass2 : BaseClass 
{ 
    public override void SetLabels() 
    { 
     //Set the label here 
    } 
}