用户控件、服务器控件和自定义控件之间有什么区别?

时间:2022-05-12 15:27:31

I thought I had reasonable answers for this question at a recent interview, but I bombed it. :(

在最近的一次采访中,我认为我对这个问题有合理的答案,但我对此进行了轰炸。 :(

  • What are the major differences between the three?
  • 这三者之间有哪些主要区别?
  • If not obvious by the answer to the previous bullet, when would you choose one over the other?
  • 如果前一个子弹的答案不明显,你什么时候选择一个?

3 个解决方案

#1


24  

  • User Controls are controls built with a designer within a web project. They typically are only private to a web application (Although there are ways you can make them available to other projects).

    用户控件是在Web项目中使用设计器构建的控件。它们通常仅对Web应用程序是私有的(尽管有一些方法可以将它们提供给其他项目)。

  • Server Controls are controls that are also known as Web Controls. These are reusable controls that render their html without the aid of a designer, they are created in a seperate assembly from the web application, are appropiate for controls which will be used in many different web applications

    服务器控件是也称为Web控件的控件。这些是可重用的控件,无需设计人员的帮助即可呈现其html,它们是在Web应用程序的单独程序集中创建的,适用于将在许多不同的Web应用程序中使用的控件

  • Composite Controls are a sub type of Web Controls and are controls which are made up (composed) of other web controls.

    复合控件是Web控件的子类型,是由其他Web控件组成(组成)的控件。

I've never heard of a custom control to mean anything other then a control which is custom built by you or your team. And it could include user, web or composite controls.

我从未听说过自定义控件是指除了您或您的团队定制的控件之外的任何其他控件。它可以包括用户,Web或复合控件。

#2


7  

A User Control is a partial web page, created in the same way as any other web page in ASP.NET, except that it has an .ASCX extension, and it can be embedded in your other ASPX pages.

用户控件是一个部分网页,其创建方式与ASP.NET中的任何其他网页相同,但它具有.ASCX扩展名,并且可以嵌入到其他ASPX页面中。

User controls are registered with the web page in which they are used, like this:

用户控件在使用它们的网页中注册,如下所示:

<%@ Register TagPrefix="UC" TagName="TestControl" Src="test.ascx" %>

They are then declared in the web page they are to be used in, like this:

然后在他们将要使用的网页中声明它们,如下所示:

<UC:TestControl id="Test1" runat="server"/>

Custom controls are compiled code components that execute on the server, expose the object model, and render markup text, such as HTML or XML, as a normal Web Form or user control does. Custom controls are written in C# or VB, and are derived from the class

自定义控件是在服务器上执行的已编译代码组件,公开对象模型,并将标记文本(如HTML或XML)呈现为普通Web窗体或用户控件。自定义控件使用C#或VB编写,并从类中派生

System.Web.UI.WebControls.WebControl 

Server controls are controls that execute on the server and render markup to the browser. User controls and custom controls are both examples of server controls.

服务器控件是在服务器上执行并将标记呈现给浏览器的控件。用户控件和自定义控件都是服务器控件的示例。

http://support.microsoft.com/kb/893667

http://support.microsoft.com/kb/893667

#3


1  

Difference between Web User Controls and Web Custom Controls in ASP.Net.

ASP.Net中Web用户控件和Web自定义控件之间的区别。

Web user control:

网络用户控制:

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. 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. 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用户控件很容易制作,但在高级场景中使用它们可能不太方便。您开发Web用户控件几乎与开发Web窗体页面的方式相同。与Web窗体一样,用户控件可以在可视化设计器中创建,可以使用与HTML分离的代码编写,并且可以处理执行事件。但是,因为Web用户控件是在运行时动态编译的,所以它们无法添加到工具箱中,并且在添加到页面时由简单的占位符字形表示。如果您习惯于完整的Visual Studio .NET设计时支持(包括“属性”窗口和“设计”视图预览),则会使Web用户控件更难使用。此外,在应用程序之间共享用户控件的唯一方法是在每个应用程序中放置一个单独的副本,如果对控件进行更改,则需要更多维护。

Web custom control:

Web自定义控件:

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

Web自定义控件是编译代码,这使它们更易于使用,但更难创建;必须在代码中创作Web自定义控件。但是,创建控件后,可以将其添加到工具箱中,并将其显示在具有完整“属性”窗口支持和ASP.NET服务器控件的所有其他设计时功能的可视化设计器中。此外,您可以在全局程序集缓存中安装Web自定义控件的单个副本,并在应用程序之间共享它,这使维护更容易。

Read more detailed article here.

在这里阅读更详细的文章。

#1


24  

  • User Controls are controls built with a designer within a web project. They typically are only private to a web application (Although there are ways you can make them available to other projects).

    用户控件是在Web项目中使用设计器构建的控件。它们通常仅对Web应用程序是私有的(尽管有一些方法可以将它们提供给其他项目)。

  • Server Controls are controls that are also known as Web Controls. These are reusable controls that render their html without the aid of a designer, they are created in a seperate assembly from the web application, are appropiate for controls which will be used in many different web applications

    服务器控件是也称为Web控件的控件。这些是可重用的控件,无需设计人员的帮助即可呈现其html,它们是在Web应用程序的单独程序集中创建的,适用于将在许多不同的Web应用程序中使用的控件

  • Composite Controls are a sub type of Web Controls and are controls which are made up (composed) of other web controls.

    复合控件是Web控件的子类型,是由其他Web控件组成(组成)的控件。

I've never heard of a custom control to mean anything other then a control which is custom built by you or your team. And it could include user, web or composite controls.

我从未听说过自定义控件是指除了您或您的团队定制的控件之外的任何其他控件。它可以包括用户,Web或复合控件。

#2


7  

A User Control is a partial web page, created in the same way as any other web page in ASP.NET, except that it has an .ASCX extension, and it can be embedded in your other ASPX pages.

用户控件是一个部分网页,其创建方式与ASP.NET中的任何其他网页相同,但它具有.ASCX扩展名,并且可以嵌入到其他ASPX页面中。

User controls are registered with the web page in which they are used, like this:

用户控件在使用它们的网页中注册,如下所示:

<%@ Register TagPrefix="UC" TagName="TestControl" Src="test.ascx" %>

They are then declared in the web page they are to be used in, like this:

然后在他们将要使用的网页中声明它们,如下所示:

<UC:TestControl id="Test1" runat="server"/>

Custom controls are compiled code components that execute on the server, expose the object model, and render markup text, such as HTML or XML, as a normal Web Form or user control does. Custom controls are written in C# or VB, and are derived from the class

自定义控件是在服务器上执行的已编译代码组件,公开对象模型,并将标记文本(如HTML或XML)呈现为普通Web窗体或用户控件。自定义控件使用C#或VB编写,并从类中派生

System.Web.UI.WebControls.WebControl 

Server controls are controls that execute on the server and render markup to the browser. User controls and custom controls are both examples of server controls.

服务器控件是在服务器上执行并将标记呈现给浏览器的控件。用户控件和自定义控件都是服务器控件的示例。

http://support.microsoft.com/kb/893667

http://support.microsoft.com/kb/893667

#3


1  

Difference between Web User Controls and Web Custom Controls in ASP.Net.

ASP.Net中Web用户控件和Web自定义控件之间的区别。

Web user control:

网络用户控制:

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. 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. 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用户控件很容易制作,但在高级场景中使用它们可能不太方便。您开发Web用户控件几乎与开发Web窗体页面的方式相同。与Web窗体一样,用户控件可以在可视化设计器中创建,可以使用与HTML分离的代码编写,并且可以处理执行事件。但是,因为Web用户控件是在运行时动态编译的,所以它们无法添加到工具箱中,并且在添加到页面时由简单的占位符字形表示。如果您习惯于完整的Visual Studio .NET设计时支持(包括“属性”窗口和“设计”视图预览),则会使Web用户控件更难使用。此外,在应用程序之间共享用户控件的唯一方法是在每个应用程序中放置一个单独的副本,如果对控件进行更改,则需要更多维护。

Web custom control:

Web自定义控件:

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

Web自定义控件是编译代码,这使它们更易于使用,但更难创建;必须在代码中创作Web自定义控件。但是,创建控件后,可以将其添加到工具箱中,并将其显示在具有完整“属性”窗口支持和ASP.NET服务器控件的所有其他设计时功能的可视化设计器中。此外,您可以在全局程序集缓存中安装Web自定义控件的单个副本,并在应用程序之间共享它,这使维护更容易。

Read more detailed article here.

在这里阅读更详细的文章。