Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-17
7
Medium Priority
?
1,564 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 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.
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Assisted Solution

by:AngryBinary
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.
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

824 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