Use a usercontrol from another project to current

2019-08-05 00:13发布

问题:

I have created a test webform where it is under the AspPage Project. then I have 3 user controls in TestUserControl Project. Is it possible to register/add the user controls in the webpage? I have seen one tutorial where the user controls are copied in the pre compilation but it seems I get errors and that wouldn't work when published. I appreciate your help guys. Thanks in advance

回答1:

Basically user controls in ASP.NET are not design to be reused between different project. That's why if you want to have reusable controls than you can try create your controls base on CompositeControl or CompositeDataBoundControl. In this case you will able to place your controls to Class Library project and then use these controls as other standard server controls by adding reference to this project(assembly). The main difference in development composite controls vs user controls is that you need to add child controls programmatically instead of using markup. But the principals are the same. I can advice to start from reading A Crash Course on ASP.NET Control Development: Building Composite Controls