Compile web application project ascx into dll

前端 未结 6 1377
孤独总比滥情好
孤独总比滥情好 2021-01-02 04:36

Is it possible to compile a web application project .ascx (user control) into a dll?

I want to do the following:

  1. Use the same control in multiple websi
相关标签:
6条回答
  • 2021-01-02 05:05

    I had to do it once and I followed this article

    http://www.codeproject.com/KB/user-controls/EmbeddedUserControl.aspx

    It's based on the possibility of mounting a virtual file system on different places (an assembly, database, etc)

    0 讨论(0)
  • 2021-01-02 05:11

    Another way would be to convert user control to the custom control. There is an nice article on MSDN: Turning an .ascx User Control into a Redistributable Custom Control which describes exactly how to do that. Here is the summary:

    1. Write your user control as you normally would, typically using the Visual Studio designer.
    2. Test it using a simple page before trying to deploy it.
    3. Deploy the application to precompile it.
    4. Grab the user control's assembly produced by the deployment step, and you're essentially done: You have your custom control.
    5. Finally, use your custom control in other apps the same way as you always use custom controls.

    Hope this helps.

    0 讨论(0)
  • 2021-01-02 05:23

    Conversion is easy, and could even be fully automated. It simply requires changing a few settings and base classes in the DLL Project you want your ASCX controls embedded in.

    1... For each UserControl, set the ASCX file's Build Action (under Properties) to "Embedded Resource", and delete its associated designer file.

    2... Save the project.

    3... Right click the project and choose "Unload Project".

    4... Right click it again and choose the "Edit *.csproj" option.

    Change sections that look like this (where the asterisk represents your class name):

    <Compile Include="*.ascx.cs">
        <DependentUpon>*.ascx</DependentUpon>
        <SubType>ASPXCodeBehind</SubType>
    </Compile>
    

    to look like this

    <Compile Include="*.ascx.cs" />
    

    That will cause the code-behind files to be compiled independently of the ASCX files.

    5... Save changes, and right click the project and choose "Reload Project".

    6... Open all your "*.ascx.cs" files and make them inherit from the following custom UserControl class, instead of the System.Web.UI.UserControl class (you may need to locate parent classes to complete this step).

    public class UserControl : System.Web.UI.UserControl
    {
        protected override void FrameworkInitialize()
        {
            base.FrameworkInitialize();
            string content = String.Empty;
            Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( GetType().FullName + ".ascx" );
            using (StreamReader reader = new StreamReader(stream))
                content = reader.ReadToEnd();
            Control userControl = Page.ParseControl( content );
            this.Controls.Add( userControl );
        }
    }
    

    This base class will take care of loading and parsing the embedded ASCX file.

    7... Finally, you may need to place ASCX files in subfolders so that their resource names (automatically determined by folder path) match the full type name of their associated class (plus ".ascx"). Assuming your root namespace matches your project name, then a class named "ProjectName.Namespace1.Namespace2.ClassName" will need its ASCX file in a subfolder "Namespace1\Namespace2", so it gets embedded with the name "ProjectName.Namespace1.Namespace2.ClassName.ascx".

    And that's it! Once you compile the DLL and include it in another project, you can instantiate instances of your user controls using the "new" operator, like any other class. As always, your control will be "caught up" to the current page event once added as a child control to the page or another control on the page.

    0 讨论(0)
  • 2021-01-02 05:28

    It is difcult to use user controls in this way due to the markup ascx file. If you want to create reusable control libraries your are much better off creating custom controls.

    0 讨论(0)
  • 2021-01-02 05:30

    I know this is old, but yes its possible.. I do it all the time, see https://kocubinski.wordpress.com/2013/05/06/compile-asp-net-webforms-ascx-usercontrols-into-assembly-for-re-use/

    0 讨论(0)
  • 2021-01-02 05:32

    There are quite a few articles out there on how to do exactly that:

    http://www.nathanblevins.com/Articles/Compile-a-Web-User-Control-into-a-DLL-.Net-c-.aspx

    http://blogs.msdn.com/davidebb/archive/2005/10/30/487160.aspx

    0 讨论(0)
提交回复
热议问题