This document discusses ASP.Net control architecture and custom control development. It covers the different types of controls like user and server controls. It demonstrates how to create custom controls, manage their properties and state, and handle control events. The control lifecycle and best practices for performance are also discussed. Creating composite controls using existing controls is presented as a way to build more complex controls through composition.
1. Asp.Net Control Architecture Sundararajan Subramanian Software Development Engineer | Microsoft Sundararajan.Subramanian@microsoft.com
2. Agenda Introduction What is a Custom Control? Custom Controls User Controls Server Controls User Controls – Demo Server Controls – Rendering – Demo Properties & State Management Control Life Cycle. Server Controls – Composite Controls Event Handling Summary
3. What is a Server Control? .Net component that is used to generate the user interface of an ASP.NET Web application. Eg: Label, GridView, etc.,
4. Types of Controls Declarative Markup in .Aspx Page Common functionalities Refactored into a User Control - .ascx Reusable behavior built /compiled and packaged as Server Control
5. Ways to Author Custom controls User Controls Simple, declarative authoring model (.ascx file) Scoped to a single application Well suited to static content and layout “Custom” or Compiled Controls Code-based authoring model (.cs or .vb class file) Easily shared across applications Well suited to dynamic or programmatic generation of content and layout More complex, but also more capabilities
8. Which Base Class To Choose? System.Web.UI.control System.Web.UI.WebControls.WebControl Derive from Existing Controls
9. Properties Getters And Setters Allows the Client to set the properties at design time or runtime. String _Text; Public String Text{ get { return _Text; } Set { _Text=Value; } }
10. State Management View State Session State Application State Control state Override Savecontrolstate and Loadcontrolstate Custom State management Override LoadViewstate and SaveviewState
11. State Management - Sample [ Bindable(true), Category("Appearance"), DefaultValue(""), Description(“...") ] public string Text { get { object o = ViewState[“Text"]; if (o == null) return String.Empty; else return (string)o; } set { ViewState[“Text"] = value; } }
14. Raising an Event Define the EventArgs Define the Event Delegate Define a method that invokes the event Delegate public class LogOutEventArgs : EventArgs {...} public delegate void LogOutEventHandler(object sender,LogOutEventArgs e); protected virtual void OnLogOut(LogOutEventArgs e) { if (LogOut != null) { LogOut(this, e); } }
16. Composite controls Based on object composition Combining the existing controls and delegating the responsibility to the existing controls. Reuses the feature of existing controls
18. Performance Considerations Use Event Properties for handling Events Store only necessary information in State Store very minimal information in Control state
19. Summary Controls provide ways to reuse functionality in web apps Use User controls for within application usage Create Specialized derived controls to make Incremental changes to existing controls Use Composition to leverage existing controls to build more complex controls
20. Contact Contact me @ http://blogs.msdn.com/sundararajan Sundararajan.subramanian@microsoft.com