How to make user control partial classes aware of controls declared in the base class?

假装没事ソ 提交于 2019-12-12 10:49:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!