是否可以将Web用户控件添加到类库中?

时间:2021-07-28 17:59:23

I'm looking at building some web user controls with an eye toward re-use, but I can't seem to add a Web User Control in my class library in VS2008. Is there a way to work around this problem, or is there a better approach to creating reusable controls?

我正在考虑构建一些Web用户控件,着眼于重用,但我似乎无法在VS2008的类库中添加Web用户控件。有没有办法解决这个问题,还是有更好的方法来创建可重用的控件?

6 个解决方案

#1


5  

You can create either Web User Controls or Web Custom Controls that encapsulate the functionality you need. The main difference between the two controls lies in ease of creation vs. ease of use at design time.

您可以创建封装所需功能的Web用户控件或Web自定义控件。两个控件之间的主要区别在于易于创建与易于在设计时使用。

You should maybe consider creating a Web Custom Control library. There is a walkthrough for creating a web custom control using the Web Control Library template.

您应该考虑创建Web自定义控件库。有一个演练用于使用Web Control Library模板创建Web自定义控件。

According to the MSDN article "Recommendations for Web User Controls vs. Web Custom Controls" these are the differences between the two types of controls:

根据MSDN文章“Web用户控件与Web自定义控件的建议”,这两种控件之间存在差异:

Web user controls are easy to make, but they can be less convenient to use in advanced scenarios. You develop Web user controls almost exactly the same way that you develop Web Forms pages. Like Web Forms, user controls can be created in the visual designer, they can be written with code separated from the HTML, and they can handle execution events.

Web用户控件很容易制作,但在高级场景中使用它们可能不太方便。您开发Web用户控件几乎与开发Web窗体页面的方式相同。与Web窗体一样,用户控件可以在可视化设计器中创建,可以使用与HTML分离的代码编写,并且可以处理执行事件。

However, because Web user controls are compiled dynamically at run time they cannot be added to the Toolbox, and they are represented by a simple placeholder glyph when added to a page. This makes Web user controls harder to use if you are accustomed to full Visual Studio .NET design-time support, including the Properties window and Design view previews.

但是,因为Web用户控件是在运行时动态编译的,所以它们无法添加到工具箱中,并且在添加到页面时由简单的占位符字形表示。如果您习惯于完整的Visual Studio .NET设计时支持(包括“属性”窗口和“设计”视图预览),则会使Web用户控件更难使用。

Also, 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.

此外,在应用程序之间共享用户控件的唯一方法是在每个应用程序中放置一个单独的副本,如果对控件进行更改,则需要更多维护。

Web custom controls are compiled code, which makes them easier to use but more difficult to create; Web custom controls must be authored in code. Once you have created the control, however, you can add it to the Toolbox and display it in a visual designer with full Properties window support and all the other design-time features of ASP.NET server controls.

Web自定义控件是编译代码,这使它们更易于使用,但更难创建;必须在代码中创作Web自定义控件。但是,创建控件后,可以将其添加到工具箱中,并将其显示在具有完整“属性”窗口支持和ASP.NET服务器控件的所有其他设计时功能的可视化设计器中。

In addition, you can install a single copy of the Web custom control in the global assembly cache and share it between applications, which makes maintenance easier. For more information see global assembly cache.

此外,您可以在全局程序集缓存中安装Web自定义控件的单个副本,并在应用程序之间共享它,这使维护更容易。有关更多信息,请参阅全局汇编缓存

#2


2  

Follow the following steps (from this post by Phil Haacked):

按照以下步骤(来自Phil Haacked的这篇文章):

  1. Close VS.NET 2005.

    关闭VS.NET 2005。

  2. Open the directory C**:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp** (assuming a default installation of VS.NET).

    打开目录C **:\ Program Files \ Microsoft Visual Studio 8 \ Web \ WebNewFileItems \ CSharp **(假设是VS.NET的默认安装)。

  3. Open the CSharpItems.vsdir file in Notepad. Select the text and copy it to the clipboard.

    在记事本中打开CSharpItems.vsdir文件。选择文本并将其复制到剪贴板。

  4. Now open up the file C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems\CSharpItems.vsdir and paste the contents of the clipboard underneath the existing text.

    现在打开文件C:\ Program Files \ Microsoft Visual Studio 8 \ VC#\ CSharpProjectItems \ CSharpItems.vsdir并将剪贴板的内容粘贴到现有文本下面。

  5. Now copy the contents of C:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp (excluding CSharpItems.vsdir) into the folder C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems.
  6. 现在将C:\ Program Files \ Microsoft Visual Studio 8 \ Web \ WebNewFileItems \ CSharp(不包括CSharpItems.vsdir)的内容复制到文件夹C:\ Program Files \ Microsoft Visual Studio 8 \ VC#\ CSharpProjectItems中。

Now “Web User Control” should be an option when you select Add | New Item.

现在,当您选择添加|时,“Web用户控制”应该是一个选项新物品。

Reference: http://haacked.com/archive/2006/02/07/addingwebusercontroltoaclasslibraryinvs.net2005.aspx

参考:http://haacked.com/archive/2006/02/07/addingwebusercontroltoaclasslibraryinvs.net2005.aspx

#3


1  

As platte's link mentions, if you're going for reuse then Web User Controls aren't very good. The ascx file has to be manually copied to every project you want to use them in, or you have to hack your way around that.

正如普拉特的链接所提到的,如果您要重用,那么Web用户控件就不是很好。必须手动将ascx文件复制到您要使用它们的每个项目中,否则您必须自行解决这个问题。

It's better to use System.Web.UI.WebControls.WebControl which is what you get when you add an "ASP.NET Server Control". These are designed for reuse. If one of the existing framework controls fits the bill for the most part and you just need to extend the functionality of it, then add an "ASP.NET Server Control" and change it to inherit from Panel or Menu or whatever.

最好使用System.Web.UI.WebControls.WebControl,这是你添加“ASP.NET服务器控件”时得到的。这些是为重用而设计的。如果其中一个现有的框架控件大部分都符合要求并且您只需要扩展它的功能,那么添加一个“ASP.NET服务器控件”并将其更改为继承自Panel或Menu或其他任何内容。

If you're still determined to get reusable Web User Controls to work, then this article by The Gu should set you on the right path.

如果您仍然决定让可重用的Web用户控件工作,那么The Gu的这篇文章应该让您走上正确的道路。

#4


0  

There is a project template called "ASP.NET Server Control" that I assume you can use...

有一个名为“ASP.NET服务器控件”的项目模板,我假设您可以使用...

--larsw

--larsw

#5


0  

You can do anything in a class library.

您可以在类库中执行任何操作。

  1. Add reference to System.Web
  2. 添加对System.Web的引用
  3. Create your new Control class that inherits from WebControl or HtmlControl or whatever.
  4. 创建从WebControl或HtmlControl继承的新Control类。

That's it. You now have a reusable control for ASP.NET.

而已。您现在拥有ASP.NET的可重用控件。

You could do some special things like add attributes to your class and properties, but they are really not needed.

您可以执行一些特殊操作,例如向类和属性添加属性,但实际上并不需要它们。

[DefaultProperty("Text")]
[Category("...")]
[DefaultValue("")]

#6


0  

You could using virtual path providers but you you should consider whether it really is worth your wile. Consider this codeproject article on the subject.

您可以使用虚拟路径提供程序,但是您应该考虑它是否值得您的诡计。请考虑有关此主题的此代码项目文章。

#1


5  

You can create either Web User Controls or Web Custom Controls that encapsulate the functionality you need. The main difference between the two controls lies in ease of creation vs. ease of use at design time.

您可以创建封装所需功能的Web用户控件或Web自定义控件。两个控件之间的主要区别在于易于创建与易于在设计时使用。

You should maybe consider creating a Web Custom Control library. There is a walkthrough for creating a web custom control using the Web Control Library template.

您应该考虑创建Web自定义控件库。有一个演练用于使用Web Control Library模板创建Web自定义控件。

According to the MSDN article "Recommendations for Web User Controls vs. Web Custom Controls" these are the differences between the two types of controls:

根据MSDN文章“Web用户控件与Web自定义控件的建议”,这两种控件之间存在差异:

Web user controls are easy to make, but they can be less convenient to use in advanced scenarios. You develop Web user controls almost exactly the same way that you develop Web Forms pages. Like Web Forms, user controls can be created in the visual designer, they can be written with code separated from the HTML, and they can handle execution events.

Web用户控件很容易制作,但在高级场景中使用它们可能不太方便。您开发Web用户控件几乎与开发Web窗体页面的方式相同。与Web窗体一样,用户控件可以在可视化设计器中创建,可以使用与HTML分离的代码编写,并且可以处理执行事件。

However, because Web user controls are compiled dynamically at run time they cannot be added to the Toolbox, and they are represented by a simple placeholder glyph when added to a page. This makes Web user controls harder to use if you are accustomed to full Visual Studio .NET design-time support, including the Properties window and Design view previews.

但是,因为Web用户控件是在运行时动态编译的,所以它们无法添加到工具箱中,并且在添加到页面时由简单的占位符字形表示。如果您习惯于完整的Visual Studio .NET设计时支持(包括“属性”窗口和“设计”视图预览),则会使Web用户控件更难使用。

Also, 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.

此外,在应用程序之间共享用户控件的唯一方法是在每个应用程序中放置一个单独的副本,如果对控件进行更改,则需要更多维护。

Web custom controls are compiled code, which makes them easier to use but more difficult to create; Web custom controls must be authored in code. Once you have created the control, however, you can add it to the Toolbox and display it in a visual designer with full Properties window support and all the other design-time features of ASP.NET server controls.

Web自定义控件是编译代码,这使它们更易于使用,但更难创建;必须在代码中创作Web自定义控件。但是,创建控件后,可以将其添加到工具箱中,并将其显示在具有完整“属性”窗口支持和ASP.NET服务器控件的所有其他设计时功能的可视化设计器中。

In addition, you can install a single copy of the Web custom control in the global assembly cache and share it between applications, which makes maintenance easier. For more information see global assembly cache.

此外,您可以在全局程序集缓存中安装Web自定义控件的单个副本,并在应用程序之间共享它,这使维护更容易。有关更多信息,请参阅全局汇编缓存

#2


2  

Follow the following steps (from this post by Phil Haacked):

按照以下步骤(来自Phil Haacked的这篇文章):

  1. Close VS.NET 2005.

    关闭VS.NET 2005。

  2. Open the directory C**:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp** (assuming a default installation of VS.NET).

    打开目录C **:\ Program Files \ Microsoft Visual Studio 8 \ Web \ WebNewFileItems \ CSharp **(假设是VS.NET的默认安装)。

  3. Open the CSharpItems.vsdir file in Notepad. Select the text and copy it to the clipboard.

    在记事本中打开CSharpItems.vsdir文件。选择文本并将其复制到剪贴板。

  4. Now open up the file C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems\CSharpItems.vsdir and paste the contents of the clipboard underneath the existing text.

    现在打开文件C:\ Program Files \ Microsoft Visual Studio 8 \ VC#\ CSharpProjectItems \ CSharpItems.vsdir并将剪贴板的内容粘贴到现有文本下面。

  5. Now copy the contents of C:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp (excluding CSharpItems.vsdir) into the folder C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems.
  6. 现在将C:\ Program Files \ Microsoft Visual Studio 8 \ Web \ WebNewFileItems \ CSharp(不包括CSharpItems.vsdir)的内容复制到文件夹C:\ Program Files \ Microsoft Visual Studio 8 \ VC#\ CSharpProjectItems中。

Now “Web User Control” should be an option when you select Add | New Item.

现在,当您选择添加|时,“Web用户控制”应该是一个选项新物品。

Reference: http://haacked.com/archive/2006/02/07/addingwebusercontroltoaclasslibraryinvs.net2005.aspx

参考:http://haacked.com/archive/2006/02/07/addingwebusercontroltoaclasslibraryinvs.net2005.aspx

#3


1  

As platte's link mentions, if you're going for reuse then Web User Controls aren't very good. The ascx file has to be manually copied to every project you want to use them in, or you have to hack your way around that.

正如普拉特的链接所提到的,如果您要重用,那么Web用户控件就不是很好。必须手动将ascx文件复制到您要使用它们的每个项目中,否则您必须自行解决这个问题。

It's better to use System.Web.UI.WebControls.WebControl which is what you get when you add an "ASP.NET Server Control". These are designed for reuse. If one of the existing framework controls fits the bill for the most part and you just need to extend the functionality of it, then add an "ASP.NET Server Control" and change it to inherit from Panel or Menu or whatever.

最好使用System.Web.UI.WebControls.WebControl,这是你添加“ASP.NET服务器控件”时得到的。这些是为重用而设计的。如果其中一个现有的框架控件大部分都符合要求并且您只需要扩展它的功能,那么添加一个“ASP.NET服务器控件”并将其更改为继承自Panel或Menu或其他任何内容。

If you're still determined to get reusable Web User Controls to work, then this article by The Gu should set you on the right path.

如果您仍然决定让可重用的Web用户控件工作,那么The Gu的这篇文章应该让您走上正确的道路。

#4


0  

There is a project template called "ASP.NET Server Control" that I assume you can use...

有一个名为“ASP.NET服务器控件”的项目模板,我假设您可以使用...

--larsw

--larsw

#5


0  

You can do anything in a class library.

您可以在类库中执行任何操作。

  1. Add reference to System.Web
  2. 添加对System.Web的引用
  3. Create your new Control class that inherits from WebControl or HtmlControl or whatever.
  4. 创建从WebControl或HtmlControl继承的新Control类。

That's it. You now have a reusable control for ASP.NET.

而已。您现在拥有ASP.NET的可重用控件。

You could do some special things like add attributes to your class and properties, but they are really not needed.

您可以执行一些特殊操作,例如向类和属性添加属性,但实际上并不需要它们。

[DefaultProperty("Text")]
[Category("...")]
[DefaultValue("")]

#6


0  

You could using virtual path providers but you you should consider whether it really is worth your wile. Consider this codeproject article on the subject.

您可以使用虚拟路径提供程序,但是您应该考虑它是否值得您的诡计。请考虑有关此主题的此代码项目文章。