Building Custom Controls with ASP.NET and the Microsoft .NET Framework ® Rames Gantanant Microsoft Regional Director, Thailand ramesg@puumsoft.co.th What We Will Cover Developing Web custom controls Creating a control designer Control rendering Event handling Developing composite controls Session Prerequisites Basic knowledge of C# Basic knowledge of EventHandling in .NET Familiarity with the .NET Framework Understanding of object-oriented programming Basic HTML / WYSIWYG experience Level 200 So Why This Presentation? Benefits of designing custom controls Code reuse Quick page development Make easy changes to the site through controls Agenda Web user controls versus custom controls Creating a simple Web custom control Implementing a control designer for designtime rendering Rendering methods and how to use them Handling postback events Developing a composite control Agenda Web user controls versus custom controls Creating a simple Web custom control Implementing a control designer for designtime rendering Rendering methods and how to use them Handling postback events Developing a composite control Web User Controls Versus Custom Controls Web user controls are compiled dynamically at run time Web user controls cannot be added to the Toolbox The only way to share a Web user control between applications is to put a separate copy in each application Custom controls have full visual design tool support for consumers Creating a Simple Custom Control Inheriting from WebControl versus Control Consuming the control in a Web application Adding a custom property to the control Demonstration 1 Creating a Simple Custom Control Agenda Web user controls versus custom controls Creating a simple Web custom control Implementing a control designer for designtime rendering Rendering methods and how to use them Handling postback events Developing a composite control Creating a Simple Web Custom Control WebControl provides typical Web user control properties Adding the control and Web application project within the same solution helps with developing and debugging The control can be added to the toolbox and even to the global assembly cache, allowing ease of use for multiple developers Creating a Simple Web Custom Control Creating an icon for the control helps distinguish it when added to the toolbox Agenda Web user controls versus custom controls Creating a simple Web custom control Implementing a control designer for designtime rendering Rendering methods and how to use them Handling postback events Developing a composite control Demonstration 2 Implementing a Control Designer for Design-Time Rendering Implementing a Control Designer for Design-Time Rendering System.Design.dll provides the base class for design-time rendering GetDesignTimeHtml() overrides the default design-time rendering output Design-time and run-time HTML output are separate entities Designer class is specified in the controls attribute list Agenda Web user controls versus custom controls Creating a simple Web custom control Implementing a control designer for designtime rendering Rendering methods and how to use them Handling postback events Developing a composite control Demonstration 3 Rendering Methods and How to Use Them Rendering Methods and How to Use Them Override the Render() method when inheriting from Control Override RenderContents() method when inheriting from WebControl Rendering Methods and How to Use Them Default “base-element” for a control is the <span> element Specify which base-class constructor should be called when creating instances of the derived class, such as base(HtmlTextWriterTag.A) for an <a> element Use AddAttributesToRender() method to add HTML attributes and styles to the control Agenda Web user controls versus custom controls Creating a simple Web custom control Implementing a control designer for designtime rendering Rendering methods and how to use them Handling postback events Developing a composite control Demonstration 4 Handling Postback Events Handling Postback Events IPostBackEventHandler defines the method ASP.NET server controls must implement to handle postback events Define the click event Invoke delegates registered with the click event Define the method of IPostBackEventHandler that raises change events Handling Postback Events Page.GetPostBackEventReference() obtains a reference to a client-side script function that causes, when invoked, the server to post back to the page Agenda Web user controls versus custom controls Creating a simple Web custom control Implementing a control designer for designtime rendering Rendering methods and how to use them Developing a composite control Demonstration 5 Developing a Composite Control Developing a Composite Control INamingContainer interface identifies a container control that creates a new ID namespace within a Page object's control hierarchy (that is, it will provide a unique namespace for any server controls that it contains in the .aspx file) EnsureChildControls() determines whether the server control contains child controls. If it does not, it creates child controls Developing a Composite Control CreateChildControls() notifies server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering Developing a Composite Control Main Differences Between a Web User Control and a Custom Control Minimal design-time support for authoring Authored programmatically in an objectoriented programming language that targets the common language runtime Compiled and persisted as an assembly (.dll) Session Summary WebControl derives from Control and exposes properties common to Web user controls Adding the control and Web application project in the same solution makes it easier to debug, rebuild, and test while developing The control can be added to the toolbox and even to the global assembly cache to make it easy for multiple developers to use on different machines Session Summary, Continued Composite controls are compiled and persisted as an assembly (.dll) RaisePostBackEvent enables a server control to process an event raised when a form is posted to the server For More Information… ® MSDN Web site at msdn.microsoft.com ASP.NET www.asp.net Microsoft, Windows, the Windows logo, MSDN, Visual Basic .NET, Visual Studio .NET, Microsoft Press, are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners.