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

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.

What others have said

Requesting Gravatar... jon Feb 08, 2006 11:44 AM
# re: Adding Web User Control To A Class Library In VS.NET 2005
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!
Requesting Gravatar... Wessam Zeidan Feb 13, 2006 1:55 PM
# re: Adding Web User Control To A Class Library In VS.NET 2005
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.
Requesting Gravatar... Simon Aug 22, 2006 6:10 AM
# re: Adding Web User Control To A Class Library In VS.NET 2005
Yes.Same to Wessam Zeidan
Requesting Gravatar... Hamed Abdel-Haq Nov 21, 2006 4:39 AM
# re: Adding Web User Control To A Class Library In VS.NET 2005
I faced the same proplem that Wessam faced
Requesting Gravatar... Daniel Pratte Nov 30, 2006 6:49 AM
# re: Adding Web User Control To A Class Library In VS.NET 2005
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..

ex:

class MyUserWebControl : System.Web.UI.UserControl

Good luck ;)
Requesting Gravatar... sarah Mar 15, 2007 2:25 PM
# re: Adding Web User Control To A Class Library In VS.NET 2005
nice solution!
Requesting Gravatar... Swathi Mar 27, 2007 11:36 PM
# re: Adding Web User Control To A Class Library In VS.NET 2005
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

Regards,
Swathi
Requesting Gravatar... Doug Jan 03, 2008 6:12 AM
# re: Adding Web User Control To A Class Library In VS.NET 2005
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
</script>


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: http://geekswithblogs.net/dotnetrodent/archive/2006/06/16/82136.aspx

Requesting Gravatar... Bill Dodd Mar 01, 2008 11:18 AM
# re: Adding Web User Control To A Class Library In VS.NET 2005
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-->
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{F184B08F-C81C-45F6-A57F-5ABD9991F28F}</ProjectTypeGuids>

Add it below <ProjectGuid>

Good Luck in your Module Development!
Bill Dodd
Gulf Technology Company
http://www.gulf-technology.com
bill.dodd@gulf-technology.com
Requesting Gravatar... Cam Apr 15, 2008 3:22 PM
# re: Adding Web User Control To A Class Library In VS.NET 2005
Thanks Bill, That worked perfectly! and in VS.NET 2008.
Requesting Gravatar... shaun Jun 09, 2008 1:53 PM
# re: Adding Web User Control To A Class Library In VS.NET 2005
The project type guids for c# are as below.
{349c5853-65df-11da-9384-00065b846f21};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
Requesting Gravatar... Daniel Jul 06, 2008 4:49 AM
# re: Adding Web User Control To A Class Library In VS.NET 2005
Take a look at this:
msdn.microsoft.com/en-us/library/aa479318.aspx

I've tried this solution and its worked for me.

basically
-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" %>


What do you have to say?

(will show your gravatar)
Please add 7 and 7 and type the answer here: