問題描述
用戶控件、服務器控件和自定義控件之間有什麼區別? (What are the differences between User Controls, Server Controls & Custom Controls?)
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?
參考解法
方法 1:
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).
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
Composite Controls are a sub type of Web Controls and are controls which are made up (composed) of other web controls.
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.
方法 2:
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.
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
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
(by IAmAN00B、JoshBerke、Robert Harvey)