UserControl,WebControl,RenderedControl和CompositeControl有什么区别?

时间:2020-03-05 18:55:36  来源:igfitidea点击:

有什么区别,什么是正式术语,什么术语在ASP.NET 3.5中已过时?

解决方案

回答

UserControl:一个自定义控件,以.ascx结尾,由其他Web控件组成。它几乎就像是aspx网页的小版本。它由一个UI(ascx)和背后的代码组成。不能通过引用DLL在其他项目中重用。

WebControl:托管在网页或者UserControl中的控件。它由一个或者多个串联工作的类组成,并托管在aspx页或者UserControl中。 WebControl没有UI"页面",必须直接呈现其内容。通过引用其DLL,可以在其他应用程序中重用它们。

RenderedControl:不存在。可能是WebControl的同义词。可能指示控件直接写到HttpResponse而不是呈现到aspx页。

CompositeControl:介于UserControl和WebControl之间。它们像UserControls一样编码,因为它们是由其他控件组成的。没有用于控件合成的任何图形UI,并且必须由控件设计者编码对CompositeControls UI编辑的支持。合成是在后面的代码中完成的。 CompositeControls可以在WebControls等其他项目中重用。

回答

我们已经忘记了ServerControl。

以我的理解是这样的:

  • 只有两种不同的控件:UserControl和ServerControl
  • CompositeControls是一种"高级" UserControl。在Scott Guthries博客上找到更多信息。
  • 它们都是WebControl(因为它们都是从System.Web.UI.Control派生的)
  • 它们全部以任何方式呈现,所以我希望将它们全部视为呈现控件。

从MSDN:

User Control
  
  In ASP.NET: A server
  control that is authored declaratively
  using the same syntax as an ASP.NET
  page and is saved as a text file with
  an .ascx extension. User controls
  allow page functionality to be
  partitioned and reused. Upon first
  request, the page framework parses a
  user control into a class that derives
  from System.Web.UI.UserControl and
  compiles that class into an assembly,
  which it reuses on subsequent
  requests. User controls are easy to
  develop due to their page-style
  authoring and deployment without prior
  compilation.
  
  Server control 
  
  A server-side component
  that encapsulates user interface and
  related functionality. An ASP.NET
  server control derives directly or
  indirectly from the
  System.Web.UI.Control class. The
  superset of ASP.NET server controls
  includes Web server controls, HTML
  server controls, and ASP.NET mobile
  controls. The page syntax for an
  ASP.NET server control includes a
  runat="server" attribute on the
  control's tag. See also: HTML server
  control, validation server controls,
  Web server control.

回答

由于我尚无足够的声誉进行评论,因此我将其添加为答案,但它指的是上述Will的答案。

从我们包括的链接中:

Composite controls are the right tool to architect complex components in which multiple child controls are aggregated and interact among themselves and with the outside world. Rendered controls are just right for read-only aggregation of controls in which the output doesn't include interactive elements such as drop-down or text boxes.

我认为文档引用的是通过将Render方法重写为Rendered Controls而创建的UserControls。因此,它不是问题所暗示的独立类型,而是一种实现UserControl的方式。一种模式。