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:
Running aspnet_merge.exe ...
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.
- 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:
So what we have is a reusable dll contains our User Control.