Solved

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

Posted on 2010-11-17
7
1,555 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hey!! 5 33
Reading the web config for a running service on Windows 10 16 41
List<PaisEntity> - show some 1 24
tableview is not updating 1 8
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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