问题
Do we have to do something special to have ASP.NET partial classes aware of controls that are declared in our user control's base classes? The partial classes keep generating declarations for controls in the base class which mean the controls in the base class get hidden and are null.
回答1:
The CodeFileBaseClass attribute can be applied to @Page or @Control declarations to make the ASP.NET runtime aware of any controls declared in your base class.
MSDN describes it as follows:
Specifies the type name of a base class for a page and its associated code-behind class.
This attribute is optional, but when it is used the CodeFile attribute must also be present. Use this attribute when you want to implement a shared scenario, where you define common fields (and optionally, associated events) in a base class to reference the controls declared in a Web page. Because of the ASP.NET code generation model, if you defined the fields in a base class without using this attribute, at compile time new member definitions would be generated for the controls declared in the Web page (within a separate partial class stub), and your desired scenario would not work. But if you use the CodeFileBaseClass attribute to associate the base class with the page, and you make your partial class (its name is assigned to the Inherits attribute and its source file is referenced by the CodeFile attribute) inherit from the base class, then the fields in the base class will be able to reference the controls on the page after code generation.
来源:https://stackoverflow.com/questions/2386184/how-to-make-user-control-partial-classes-aware-of-controls-declared-in-the-base