Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


ASP.NET 4.0 Web User Control (ascx) Library???

Posted on 2010-11-17
Medium Priority
Last Modified: 2012-06-27
I have a handful of different websites. These websites are different is every aspect except for 1 function. This function involves a form with about a dozen or so fields. This form also looks and operates exactly the same on each website.

Instead of having the html and code for these forms duplicated on every site, I would like to create a "Web User Control" library in Visual Studio 2010. Without really hacking some things up, I cannot find a simple way to do this. I can create a custom Server Control library but since these forms contain multiple other controls, html and functionality, it would be really tedious.

I found one example where you create an empty "Web Application" project and add your ascx files in there. You then reference the published dll. This would actually work except for the ascx files themselves have to be copied over into each referencing web project.

Does anyone have any ideas? I find it hard to believe that Microsoft would not have included this ability.
Question by:dpbouchard
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 13

Accepted Solution

AngryBinary earned 2000 total points
ID: 34159897
You can do it with an ASCX control. First, though, one alternative way would be to use a Server Control project. You'd have to build your control programmatically without the aid of the designer (i.e., in the Control constructor, you'd add child controls to the "Controls" collection), but it's the most straightforward way to do it.

So, if you really want to use an ASCX template to author the control, you should start with a Web Site project, not a Web Application project, as a Web Site project gives you the publish options you want.

In the ASCX markup, you need to specify what namespace and class name your control will have (by default, it will be in the ASP namespace, and will have a name like "mycontrol_ascx"). To do this, add a ClassName attribute to the Control directive:

<%@ Control Language="C#" CodeFile="~/MyControl.ascx.cs" Inherits="MyControl" ClassName="MyNamespace.MyControl" %>

Note that there is NO relationship between the class defined in your code behind and the ClassName you set - when the control compiles, it will create the namespace and class you put in the ClassName attribute, which will extend the class created in your code behind. So, you don't have to make any special changes in your code-behind.

After creating your control, publish the web site. In the publish dialog:

- disable "Allow this precompiled site to be updatable". This means that the ASCX will be compiled now, instead of at runtime.
- enable "Use fixed naming and single page assemblies". This puts your control into its own assembly, so you won't be carrying any other web site stuff with it. If you want to include more than one user control in the assembly, however, you can leave this disabled.

Look in the output "bin" folder where the site was published for the assembly containing your control. You probably want to make a copy and rename it, since the name is going to be pretty gnarly.

Author Comment

ID: 34161832
Thanks AB, I will give this a try in the morning. I basically tried this same approach but with a Web Application not a Web Site.

Author Comment

ID: 34168694
AngryBinary, all looked like it was going to go well but I got the following when I ran the project.

Could not load file or assembly 'Testing' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

FYI... For testing purposes I used "MyNamespace.MyControl" for the ClassName attribute. And I renamed the actual dll to "Testing". When I reference that dll in the project it actually shows up as "Testing" and "Testing" shows up in the error above.

This happens by just referencing the dll before adding any code the the source project.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 13

Assisted Solution

AngryBinary earned 2000 total points
ID: 34170939
That's odd. Maybe you shouldn't rename the DLL? I wouldn't have expected that to cause a problem, but that's the one thing I didn't do when I tested this solution; it may have been a bad assumption.
LVL 13

Expert Comment

ID: 34170952
That does make sense, actually. Try it without renaming the assembly to make sure that it works. If you want to rename the assembly after that, you have to disassemble and edit the manifest to match.

Author Comment

ID: 34172485
That did it! Thanks a lot!!!

Would editing the manifest be difficult?
LVL 13

Expert Comment

ID: 34177399
It's not phenomenally difficult. Visual Studio comes with the tools you need, and it's just a couple of command line arguments that need to be made. Look at the example provided here:

They don't address changing the assembly name specifically, but I think all you'd have to do is change "SatelliteAssembly" (the name of the assembly in their example) to whatever you want to name it in the following line of IL code:

.assembly /*20000001*/ SatelliteAssembly


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question