[英]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.