[英]I want to access controls of derive class from base class on onLoad() override in c# (Master page derive from one masterClass)
我定義了多個母版,並且在Form_Load事件上具有一些通用功能。 現在,我聲明一個派生自System.Web.UI.MasterPage的customMasterPage類,並將所有常用功能放入其中。 但是我想重寫onPage_Load()並想打印到Dervived類的控件中。
假設我有一個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";
}
}
怎么做。 請給我你的建議
提前致謝
基類對子類一無所知。 話雖如此,您可以將基類定義為具有可重寫的方法,派生類可以使用這些方法來設置特定的事物,例如:
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.