I don't know if this has to do with how FindControl works or how scope works. But my base class is having a hard time seeing the fields of child classes. Currently I'm planning have the derived class set a property in the base class, but there are a lot of derived classes, so that isn't a very attractive solution.
public class BasePage:Page
{
public void DoSomethingWithDerivedPageControl()
{
//foo is always null.
Control foo = FindControl("Foo");
}
}
public class DerivedPage : BasePage
{
//In real life, this is the code generated .aspx.de开发者_StackOverflowsigner.cs file.
protected Label Foo;
}
FindControl
doesn't use fields - it uses the controls which have been added to the page, and checks their ids.
Presumably your control hasn't been added to the page at the time when DoSomethingWithDerivedPageControl
is called.
It would be helpful if you could tell us what you're really trying to achieve... if all your derived types should have a control called Foo
, why not just put it in the base class to start with?
public abstract class BasePage:Page
{
abstract protected Label Foo {get;}
public void DoSomethingWithDerivedPageControl()
{
Control foo = this.Foo;
}
}
public class DerivedPage : BasePage
{
override protected Label Foo { get; set;}
}
Now, I suspect that's not wholy fulfilling your need. But, a base class doesn't/cant' know about it's children. Your only option to find a random field in a child class, is to just ignore the fact that they are base/derived, and just use reflection of it, as if it were an unrelated class.
To answer your title question, inheritance runs from base class to derived class, not the other way. A member is either defined in a base class, and inherited by the derived class, or defined in the derived class and unknown to the base.
It stands to reason that a base class referring to a child member couldn't compile, since the base class definition has no knowledge of that member.
Whoa, this is scary that your even trying to do this. The base class shouldn't know anything about the derived class.
精彩评论