C# 我想访问c中onLoad()覆盖上从基类派生类的控件(母版页从一个母版类派生)
我定义了多主机,并在Form_Load事件上具有一些通用功能。现在,我声明了一个从System.Web.UI.MasterPage派生的customMasterPage类,并将所有常见功能放入其中。但我想重写onPage_Load并将其打印到Dervive类的控件中 假设我有一个customMaster类C# 我想访问c中onLoad()覆盖上从基类派生类的控件(母版页从一个母版类派生),c#,asp.net,.net,C#,Asp.net,.net,我定义了多主机,并在Form_Load事件上具有一些通用功能。现在,我声明了一个从System.Web.UI.MasterPage派生的customMasterPage类,并将所有常见功能放入其中。但我想重写onPage_Load并将其打印到Dervive类的控件中 假设我有一个customMaster类 class customMasterClass:System:web.UI.MasterPage { override onLoad() { base(e)
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
}
}
在派生页面类的onPageLoad中,您可能必须将要更改的字段向上传递给基类中的方法。