Solved

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

Posted on 2010-11-17
7
1,557 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
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.
0
 
LVL 1

Author Comment

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

Author Comment

by:dpbouchard
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.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Assisted Solution

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

Expert Comment

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

Author Comment

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

Would editing the manifest be difficult?
0
 
LVL 13

Expert Comment

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

830 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