Solved

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

Posted on 2010-11-17
7
1,551 Views
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.
0
Comment
Question by:dpbouchard
  • 4
  • 3
7 Comments
 
LVL 13

Accepted Solution

by:
AngryBinary earned 500 total points
Comment Utility
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.
0
 
LVL 1

Author Comment

by:dpbouchard
Comment Utility
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.
0
 
LVL 1

Author Comment

by:dpbouchard
Comment Utility
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 13

Assisted Solution

by:AngryBinary
AngryBinary earned 500 total points
Comment Utility
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.
0
 
LVL 13

Expert Comment

by:AngryBinary
Comment Utility
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.
0
 
LVL 1

Author Comment

by:dpbouchard
Comment Utility
That did it! Thanks a lot!!!

Would editing the manifest be difficult?
0
 
LVL 13

Expert Comment

by:AngryBinary
Comment Utility
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:

http://www.codeproject.com/KB/msil/ManifestEdit.aspx

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


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now