generating a dll of a web usercontrol
How can I make a dll of my web application usercontrol? I have usercontrol222.ascx, but I want to make a dll o开发者_运维技巧ut of this usercontrol.
Create a project containing only your user control ("usercontrol222.ascx") and grab the control's dll from the deployment of your new project. Here's the source of this method with a more complete explanation: Turning an .ascx User Control into a Redistributable Custom Control (Notable excerpts below, see the link for the full run-down).
Step 3: Use the Publish Command to Precompile the Site
The next step is to use the new Publish command to precompile your site and turn your user control into a potential custom control. You'll find the command under Build / Publish Web Site. In the Publish dialog, do the following:
- Pick a Target Location. This is the location on your hard drive that your site will be precompiled to.
- Deselect "Allow this precompiled site to be updatable". In updatable mode, only the code behind file (if any) would get compiled, and the ascx would be left unprocessed. This is useful in some scenarios, but is not what you want here since you want the resulting DLL to be self-contained.
- Select "Use fixed naming and single page assemblies". This will guarantee that your user control will be compiled into a single assembly that will have a name based on the ascx file. If you don't check this option, your user control could be compiled together with other pages and user controls (if you had some), and the assembly would receive a random name that would be more difficult to work with.
Step 4: Finding the Resulting Custom Control
Now, using the Windows Explorer or a command-line window, let's go to the directory you specified as the target so we can see what was generated. You will see a number of files there, but let's focus on the one that is relevant to our goal of turning the user control into a custom control.
In the "bin" directory, you will find a file named something like App_Web_MyTestUC.ascx.cdcab7d2.dll. You are basically done, as this file is your user control transformed into a custom control! The only thing that's left to do is to actually use it.
You cannot. User controls are for the simplified scenario where you do not want to create a custom control. They have the disadvantage that the .ascx file and any other artifacts (images, styles, etc) must be included in each web site that uses the user control.
If you need complete reuse between projects, then you need to create a custom control. That's not actually that hard, if you directly translate the user control into a custom control.
You may want to look at developing your own server controls. See the following similar discussion: ASP.NET Web User Control Library.
精彩评论