ASP.NET UserControl class library

前端 未结 6 1215
情话喂你
情话喂你 2021-02-08 12:11

Is it possible to create class libraries that contain UserControls so I can reuse them? If so, how? Is the markup compiled with the .dll? Thanks for any help!

6条回答
  •  南旧
    南旧 (楼主)
    2021-02-08 12:30

    You can compile both UserControls and Pages into class libraries because ultimately, this is what happens after your web site is just in time compiled. The process is a little involved because really UserControls and Pages aren't meant to be used across applications.

    From MSDN:

    The UserControl gives you the ability to create controls that can be used in multiple places within an application or organization

    http://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol.aspx

    The prefered method for controls that are going to be used across applications would be to create custom web server controls.

    If you really want to stick with UserControls though, the basic process to obtain this functionality is as follows:

    1. Create a new web application project.
    2. Develop the reuseable UserControls.
    3. Publish the site as an un-updatable* site. (uncheck Allow this site to be updatable)
    4. Copy the compiled library from the bin directory and the ascx files as needed from the published site into the new site.

      • The un-updatable option is what brings the markup into the assembly. This is an important step.

    Yes, as point number four states, you do need to copy the ascx files. The markup will be contained in the class library and the ascx will actually be empty. There is no way to avoid this (unless you use custom web server controls) because UserControls are added to Pages through their file names.

    All of this is documented in more detail over at MSDN,

    http://msdn.microsoft.com/en-us/library/aa479564.aspx

提交回复
热议问题