You want to create some shared ASP.NET controls for use in multiple applications.
Probably the only way you think of is custom control:
the only way to share the user control between applications is to put a separate copy in each application, which takes more maintenance if you make changes to the control (MSDN)
But it isn't actually true.
You can use "Web Deployment Projects" add-in to compile your user controls project into dll which can be referenced from any web application.
Now let's take it step by step:
(make sure you have installed the "Web Deployment Projects" add-in)
- Create a new web project (SharedUserControls.csproj).
- Remove the "Default.aspx" page and web.config file.
- Add a user control (let's call it MyControl.ascx).
- Add what ever web controls you want to your user control.
- Add Web Deployment Project by standing on the project and selecting "Add Web Project Project..." from the Build menu
- Keep the default name, and click OK.
A new project was created with out any file inside
- Double click the new project to get the project property pages.
- Uncheck the last check box - "Allow this precompiled site to be updatable".
This is an important step - If you'll skip it, you'll get: ASPNETMERGE : warning 1013: Cannot find any assemblies that can be merged in the application bin folder. - Go to the "Output Assemblies" tab
- Select "Merge all pages and control outputs to a single assembly"
- Call the assembly name: "SharedUserControlMerged"
- Build the solution, and see what you get in the Output:
aspnet_compiler.exe...
Running aspnet_merge.exe ...
aspnet_merge.exe...
Successfully merged...
What happens is that the "Deployment Web Project" actually compiles the aspx and ascx files - using aspnet_compiler.
Then merge all dlls (currently we have only one - because we have only one control) using aspnet_merge (which uses ILMerge behind the scenes). - Now add another Web Project (WebApp.csproj).
- Add reference to the SharedUserControlMerged.dll from the bin folder of the SharedUserControls.csproj_deploy project.
- Register this assembly inside a web page on WebApp project.
<%@Register
tagprefix="xyz"
namespace="ASP"
assembly="SharedUserControlMerged"%>
- Now you can use your control on this page:
You need first to find out what is the name of the control in the merged dll.
I have used Reflector to do it:
and just put it in the page:
<xyz:mycontrol_ascx runat="Server"></xyz:mycontrol_ascx>
So what we have is a reusable dll contains our User Control.
Next time we will find out how to create user control with images and JavaScript files.
14 comments:
this is a great article for how to generate dll and reuse it another application
please can you tell me how can i generate dll with javascript and images
thanks a lot in advance
Thanks a lot yaar , great article,
everything is working fine but I couldn't view the output I mean no controls has been load on page, what might be the issue.
could u provide me the source code r what else I need to do
Thanks in advance,
mail: sendil.bonanzo@gmail.com
Thanks, but also
for me elements of control can't load.
*bump*
anyone getting this to work, so the controls is actually visible on the host page??
Does VS2008 anything to do with it?
We have been getting that warning forever...
aspnet_merge : warning 1013: Cannot find any assemblies that can be merged ...
Thank you!
It's a really great article. Much better than MSDN :). Everything works as magic. Controls are visible in hosting web application. One issue I had that easily resolved. Hosting application needs references to original and merged assembly.
you promised to write about how to use images and JavaScript, when you write about it? :)
So long time...
Anyway - the concept is to emmbbed the javascript and images into the dll, then call it from there.
See MSDN for more info:
http://msdn.microsoft.com/en-us/library/bb398930.aspx
Great post! More than two years have passed but it still helps people! Thank you so much!
Thanks for a great article, still valid with VS 2010
como puedo compartir mis user control a mas de un website. en visual basic 2005.
cant find this "web deploy ad-in" in VS 2010 :(
see also:
ASP.NET: Reusing Web User Controls and Forms
http://www.cmswire.com/cms/tips-tricks/aspnet-reusing-web-user-controls-and-forms-000915.php
Awesome man, i just wasted a full day for registering custom user control.
Very Very Thanks!
Post a Comment