Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
'm looking to create an instance of a user control programatically in a codebehind of a web site*. The only ways I've found to reference the class of the user control programatically have been:

Register the control in the web.config and drop the control on the page (works, but I don't want the control on the page as I want to dynamically add it).
Add a <%@ Reference Control="Name.aspx"> to the aspx page at the top.
Ideally I would want to have any aspx page reference the control without having to put the <%@ Reference> declaration at the top of each page. I know I can "Register" controls in the web.config so I don't have to do that on every page, so I'm wondering if something similar exists for references. I'm almost certain I'm missing something about the whole registering/referencing concept, so perhaps just an explanation of what the best way to do this is in order.
Posted

Add the DLL containing your UserControl to the Bin folder. You will be able to use it as if you had added a reference.
 
Share this answer
 
XML
<?xml version="1.0"?>

<configuration>

  <system.web>

    <pages>
      <controls>
        <add tagPrefix="scottgu" src="~/Controls/Header.ascx" tagName="header"/>
        <add tagPrefix="scottgu" src="~/Controls/Footer.ascx" tagName="footer"/>
        <add tagPrefix="ControlVendor" assembly="ControlVendorAssembly"/>
      </controls>
    </pages>

  </system.web>

</configuration>



http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx[^]
 
Share this answer
 
v2
Comments
jagdish123061 12-Mar-14 9:37am    
thnx but it can't be used to add user control dynamically to page. I want to add in dynamically

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900