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.