Article ID: 893667 - Last Review: November 14, 2007 - Revision: 1.8 Overview of user controls vs. custom controlsASP.NET Support Voice columnOverview of user controls vs. custom controlsTo customize this column to your needs, we want to invite you to submit your ideas about topics that interest you and issues that you want to see addressed in future Knowledge Base articles and Support Voice columns. You can submit your ideas and feedback using the Ask For It (http://support.microsoft.com/common/survey.aspx?scid=sw;en;1176&p0=&p1=&p2=&p3=&p4=) form. There's also a link to the form at the bottom of this column.On This PageIntroductionHi! This is Parag, and I am a support engineer working with the Microsoft ASP.NET support group for more than a year now. Prior to joining Microsoft, I worked on Web-based projects and desktop applications using Microsoft technologies. While providing quality support to customers, I have seen cases where there was some confusion around custom controls, and I would just like to take some time to explain some concepts around custom controls. As bad as it looks, believe me, once you get the hang of it, you will be in a better position to appreciate ASP.NET.
OverviewIn this month's column, I'll discuss the following topics:
What are user controls?User controls are custom, reusable controls, and they use the same techniques that are employed by HTML and Web server controls. They offer an easy way to partition and reuse common user interfaces across ASP.NET Web applications. They use the same Web Forms programming model on which a Web Forms page works. For more details about the Web Forms programming model, visit the following Microsoft Developer Network (MSDN) Web sites:Introduction to Web Forms pages http://msdn2.microsoft.com/en-us/library/65tcbxz3(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/65tcbxz3(vs.71).aspx) Web Forms code model http://msdn2.microsoft.com/en-us/library/015103yb(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/015103yb(vs.71).aspx) How to create a user controlThe syntax you use to create a user control is similar to the syntax you use to create a Web Forms page (.aspx). The only difference is that a user control does not include the <html>, <body>, and <form> elements since a Web Forms page hosts the user control. To create a user control, follow these steps:
How to use a user control in a Web Forms page
How to create an instance of a user control programmatically in the code behind file of a Web Forms pageThe previous example instantiated a user control declaratively in a Web Forms page using the @ Register directive. However, you can instantiate a user control dynamically and add it to the page. Here are the steps for doing that:
How a user control is processedWhen a page with a user control is requested, the following occurs:
What are custom controls?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.How to choose the base class for your custom controlTo write a custom control, you should directly or indirectly derive the new class from the System.Web.UI.Control class or from the System.Web.UI.WebControls.WebControl class:
In brief, the Control class provides the basic functionality by which you can place it in the control tree for a Page class. The WebControl class adds the functionality to the base Control class for displaying visual content on the client computer. For example, you can use the WebControl class to control the look and styles through properties like font, color, and height. How to create and use a simple custom control that extends from System.Web.UI.Control using Visual Studio
How to expose properties on the custom controlI will build on the previous example and introduce one or more properties that can be configured while using the custom control on the Web Forms page.The following example shows how to define a property that will display a message from the control a certain number of times, as specified in the property of the control:
How to apply design-time attributes on the custom controlWhy design-time attributes are neededThe custom control that you built in the previous example works as expected. However, if you want to use that control in Visual Studio, you may want the NoOfTimes property to be automatically highlighted in the Properties window whenever the custom control is selected at design time.To make this happen, you need to provide the metadata information to Visual Studio, which you can do by using a feature in Visual Studio called attributes. Attributes can define a class, a method, a property, or a field. When Visual Studio loads the custom control's class, it checks for any attributes defined at the class, method, property, or field level and changes the behavior of the custom control at design time accordingly. To find more information about attributes, visit the following MSDN Web site: http://msdn2.microsoft.com/en-us/library/Aa288059(VS.71).aspx
(http://msdn2.microsoft.com/en-us/library/Aa288059(VS.71).aspx)
Let's build a sample that uses commonly used attributes:
What are the basic differences between user controls and custom controls?Now that you have a basic idea of what user controls and custom controls are and how to create them, let's take a quick look at the differences between the two.Collapse this table
Advanced topicsNext, let's take a look at a few of the advanced features that you may use while developing custom controls.State managementWeb applications are built on HTTP, which is stateless. A page and its child controls are created on every request and are disposed of after the request is over. To maintain state in classic ASP programming, you use session and application objects. But for that, you need to do lots of coding. To avoid this, ASP.NET provides a mechanism known as view state for maintaining state across several requests. To learn more about state management and view state, visit the following MSDN Web sites:Introduction to Web Forms state management http://msdn2.microsoft.com/en-us/library/75x4ha6s(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/75x4ha6s(vs.71).aspx) The ASP.NET view state http://msdn.microsoft.com/msdnmag/issues/03/02/cuttingedge/default.aspx (http://msdn.microsoft.com/msdnmag/issues/03/02/cuttingedge/default.aspx) Saving Web Forms page values using view state http://msdn2.microsoft.com/en-us/library/4yfdwycw(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/4yfdwycw(vs.71).aspx) Example using view state in a custom controlViewStateExample.csExample using the previous control on a Web Forms pageViewStateExampleDemo.aspxRenderingIn this section, I'll briefly describe what methods you should override when you derive a custom control from either the Control class or the WebControl class.Rendering methods of the System.Web.UI.Control classFor information about the rendering methods of the System.Web.UI.Control class, visit the following MSDN Web sites:Control.Render method http://msdn2.microsoft.com/en-us/library/system.web.ui.control.render(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.web.ui.control.render(vs.71).aspx) Control.RenderControl method http://msdn2.microsoft.com/en-us/library/system.web.ui.control.rendercontrol(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.web.ui.control.rendercontrol(vs.71).aspx) Control.RenderChildren method http://msdn2.microsoft.com/en-us/library/system.web.ui.control.renderchildren(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.web.ui.control.renderchildren(vs.71).aspx) How a control is rendered on the pageEvery page has a control tree that represents a collection of all the child controls for that page. To render the control tree, an object of the HtmlTextWriter class is created that contains the HTML to be rendered on the client computer. That object is passed to the RenderControl method. In turn, the RenderControl method invokes the Render method. Then, the Render method calls the RenderChildren method on each child control, making a recursive loop until the end of the collection is reached. This process is best explained by the following example code.Rendering methods of the System.Web.UI.WebControl classFor information about the rendering methods of the System.Web.UI.WebControl class, visit the following MSDN Web sites:WebControl.RenderBeginTag method http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.renderbegintag(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.renderbegintag(vs.71).aspx) WebControl.RenderContents method http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.rendercontents(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.rendercontents(vs.71).aspx) WebControl.RenderEndTag method http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.renderendtag(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.renderendtag(vs.71).aspx) How the rendering of the WebControl class takes placeThe following code example shows the Render method for the custom control.ConclusionThat's all for now on user controls and custom controls in ASP.NET 1.0 and ASP.NET 1.1. I hope that this column helps you understand the basic differences between them and the various approaches you can take to develop them.Thank you for your time. We expect to write more about the advanced topics for custom controls, such as state management, control styles, composite controls, and design-time support for custom controls, in the near future. For more information about controls, visit the following MSDN Web sites: ASP.NET server control development basics http://msdn2.microsoft.com/en-us/library/aa310918(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/aa310918(vs.71).aspx) An extensive examination of user controls http://msdn2.microsoft.com/en-us/library/ms972975.aspx (http://msdn2.microsoft.com/en-us/library/ms972975.aspx) Building templated custom ASP.NET server controls http://msdn2.microsoft.com/en-us/library/Aa478964.aspx (http://msdn2.microsoft.com/en-us/library/Aa478964.aspx) Events in ASP.NET server controls http://msdn2.microsoft.com/en-us/library/aa720049(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/aa720049(vs.71).aspx) Composite control vs. user control http://msdn2.microsoft.com/en-us/library/aa719735(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/aa719735(vs.71).aspx) Developing ASP.NET server controls http://msdn2.microsoft.com/en-us/library/aa719973(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/aa719973(vs.71).aspx) Developing custom controls: Key concepts http://msdn2.microsoft.com/en-us/library/aa720226(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/aa720226(vs.71).aspx) Adding design-time support to ASP.NET controls http://msdn2.microsoft.com/en-us/library/Aa478960.aspx (http://msdn2.microsoft.com/en-us/library/Aa478960.aspx) As always, feel free to submit ideas on topics you want addressed in future columns or in the Knowledge Base using the Ask For It
(http://support.microsoft.com/common/survey.aspx?scid=sw;en;1176&p0=&p1=&p2=&p3=&p4=)
form.
| Article Translations
|
Back to the top
