Adding Web User Control To A Class Library In VS.NET 2005

0 comments suggest edit

If you’ve started on module development with DotNetNuke 4.0 and above in Visual Studio.NET 2005, you might run into a problem with trying to add a Web User Control (*.ascx file) to a class library.

The fix is similar to what you had to do with Visual Studio.NET 2003.

For C#, follow these steps.

  1. Close VS.NET 2005.
  2. Open the directory C:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp (assuming a default installation of VS.NET).
  3. Open the CSharpItems.vsdir file in Notepad. Select the text and copy it to the clipboard.
  4. Now open up the file C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems\CSharpItems.vsdir and paste the contents of the clipboard underneath the existing text.
  5. Now copy the contents of C:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp (excluding CSharpItems.vsdir) into the folder C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems.

Now “Web User Control” should be an option when you select Add | New Item.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.



14 responses

  1. Avatar for jon
    jon February 8th, 2006

    you can also just add the user control to the DNN project (since it is a web project rather than a class) and then drag and drop it to where it's needed. This is taking for granted that you are opening the desktop modules solution when you are developing new modules...

    Thanks for the hack though!

  2. Avatar for Wessam Zeidan
    Wessam Zeidan February 13th, 2006

    Hello Phil,

    I just tried your hack to add web usercontrols to a class library, it works fine, but there are a couple of things that don't work. First, when you add a web usercontrol to the class library, you can't tell it to place the code in a seperate file. So what I did was I created a temp website, created the usercontrol there, and then copied the files to the class library project. This places the ascx and ascx.cs files in the class library project, but they will not be linked, although double clicking on a button control in the ascx file for instance will generate the correct event handler in the ascx.cs file. The other thing is that you can't get a reference to the controls that are defined in the usercontrol from the codebehind file.

  3. Avatar for Simon
    Simon August 21st, 2006

    Yes.Same to Wessam Zeidan

  4. Avatar for Hamed Abdel-Haq
    Hamed Abdel-Haq November 20th, 2006

    I faced the same proplem that Wessam faced

  5. Avatar for Daniel Pratte
    Daniel Pratte November 29th, 2006

    I had the same problem, but I ended up with a good solution.. you don't wanna add directrly some WebUserControl to the class library, you just create a usual Class File, but you enherit it from System.Web.UI.UserControl.. So it will BE a WebUserControl and you will be able to add whatever you need in it..
    class MyUserWebControl : System.Web.UI.UserControl
    Good luck ;)

  6. Avatar for sarah
    sarah March 15th, 2007

    nice solution!

  7. Avatar for Swathi
    Swathi March 27th, 2007

    HI Daniel,
    I tried the way you suggested but I could not overcome the problem of accessing the controls in the .cs file.
    Actually I have a DotNetNuke website and a class library project in my solution. DotNetNuke accepts only usercontrols to be added in it. But I am unable to access my controls designed in the .ascx file in my .ascx.cs file. Please anyone provide me a better solution

  8. Avatar for Doug
    Doug January 2nd, 2008

    Neat solution, but it does not meet my needs. So others don't waste their time, what Wessam means is the ascx file gets one of these:

    <%@ Control Language="C#" ClassName="WebUserControl1" %>
    <script runat="server">
    put c# code here

    Where the C# code is embedded in the ascx.
    Also, Wessam's solution did not work well for me. The designer adds events to the .cs file, but the associated partial control.ascx.designer.cs does not get managed by VS.
    There seems to be some promising information here:

  9. Avatar for Bill Dodd
    Bill Dodd March 1st, 2008

    Thanks for posting this -- I wanted to add what I've found to be a good solution.
    As a module developer, I've struggled with this as well. Building your modules as a class library makes it convenient and quick to test your module progress but the class library output type jacks up your ability to add web user control's to the project.
    The BEST solution is to open the project file for your module in a text editor-- find this line or add it-->
    Add it below <ProjectGuid>
    Good Luck in your Module Development!
    Bill Dodd
    Gulf Technology Company

  10. Avatar for Cam
    Cam April 15th, 2008

    Thanks Bill, That worked perfectly! and in VS.NET 2008.

  11. Avatar for shaun
    shaun June 9th, 2008

    The project type guids for c# are as below.

  12. Avatar for Daniel
    Daniel July 5th, 2008

    Take a look at this:
    I've tried this solution and its worked for me.
    -create web user control with code embedded in script tag ie. no code behind
    -define classname attribute with a namespace prefix ie. <%@ Control Language="C#" ClassName="DKCS.FileUploadManager"%>
    -publish your site (with certain settings) this will generate individual dlls for each of your controls
    -add reference to dlls in your website
    -register with these attributes: <%@ Register TagPrefix="DKCS" Namespace="DKCS" Assembly="App_Web_fileuploadmanager.ascx.cdcab7d2" %>

  13. Avatar for Corna
    Corna March 8th, 2010

    How do I do this in VS.NET 2008?

  14. Avatar for Nikhil P
    Nikhil P July 12th, 2010

    I know its an Old thread, but its better late than never.
    The Simples way
    Refer Turning an .ascx User Control into a Redistributable Custom Control from
    My Additions / Tips to it:
    * Use Web Site and NOT Web Application
    * Add a Web Deployment Project and set properties to it as needed.
    * Just Build the Deployment project & its done. Get the DLL form the Built path in reference or copy it anywhere as needed to use.