locked
Add a User Control to another UserControl in Web Application RRS feed

  • Question

  • User891973608 posted

    I am writing a Web Application in asp.net 4.0. I have a Solution say ControlLib, where a have my User Control UC_A. I have another Solution say MainLib, which contain a User Control UC_B. I have added the .ascx,.dll and pdb file of my ControlLib to my MainLib. Now a have to use UC_A in UC_B. Please tell me how can i achieve this.

    Thanks in Advance

    Thursday, December 6, 2012 1:42 AM

Answers

  • User1423713461 posted

    My first option if this user control will be reused a lot is definitely to create a "Custom Control" rather than a user control. A custom control would allow you to simply reference you dll and the control is available for use in the project.

    If however you don't want to go that route, since it is more difficult and time consuming you can reference a user control. Build ControlLib, copy the dll and .ascx over (as you have done), then reference the .ascx as if you would if it were part of your MainLib solution! It does work, I've done it before.

    Just incase you weren't awere of this, the reason you need the .ascx as well is that when you compile you assembly, a User Control's markup does not come along in the assembly, but only the code behind (your class). So if you view your assembly in Object Browser, you see that your user control's class is there, but it has no markup / front end, so if you referenced it, you would get a NullReferenceException for any instances to controls on your user control.

    <%@ Register Src="~/Uc_A.ascx" TagName="Uc_A" TagPrefix="cc" %>

    ...

    <cc:Uc_A ID="MyUc" runat="server"></cc:Uc_A>

    Check the following, might help you figure out what's wrong:

    • Since you've copied UC_A into Control lib, have you actually added it to MainLib (i.e. Right --> Add existing item)
    • Is your .ascx class in MainLib identicle to the .ascx in ControlLib
    • Check if the ControlLib.dll is in the MainLib bin folder
    • Check that your reference to the usercontrol is correct (view code snippet above)

    Hope this helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 6, 2012 3:04 AM