210 likes | 345 Vues
In this session, we will explore the concept of custom controls in ASP.NET, covering both User and Server Controls. You'll learn about the different types of controls, their lifecycles, and best practices for state management. We will conduct demonstrations showcasing both User Controls (.ascx) and Server Controls, highlighting their unique capabilities and use cases. Additionally, we’ll discuss event handling and performance considerations for creating efficient, reusable components. Join us for a deep dive into the architecture of ASP.NET controls.
E N D
Asp.Net Control Architecture Sundararajan Subramanian Software Development Engineer | Microsoft Sundararajan.Subramanian@microsoft.com
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
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.,
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
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
DEMO User Controls
DEMO Custom Server control – Rendering Sample
Which Base Class To Choose? • System.Web.UI.control • System.Web.UI.WebControls.WebControl • Derive from Existing Controls
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; • } • }
State Management • View State • Session State • Application State • Control state • Override Savecontrolstate and Loadcontrolstate • Custom State management • Override LoadViewstate and SaveviewState
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; } }
Control Life Cycle Post Back Only
Control Life Cycle …. Post Back Only
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); } }
DEMO Raising Events
Composite controls • Based on object composition • Combining the existing controls and delegating the responsibility to the existing controls. • Reuses the feature of existing controls
DEMO Composite Control Event Handling
Performance Considerations • Use Event Properties for handling Events • Store only necessary information in State • Store very minimal information in Control state
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
Contact • Contact me @ • http://blogs.msdn.com/sundararajan • Sundararajan.subramanian@microsoft.com