ASP.NET Custom Controls by bns26590


									ASP.NET Custom
          Mike Shir
Custom Controls
   Encapsulate Rich Functionality
   Reusable
   Redistributable
Types of Custom Controls
   User Controls
   Server Controls
User Controls
   Easy to create
   Easy to use
   Lack support for design-time features
   Scoped to a single application
   Very similar to creating an ASPX page
Server Control
   Harder to author
   Done through code and not visually
   Highly customizable design-time experience
   Distributable to many applications
Creating Server Controls
   Server Controls are derived from
       System.Web.UI.Control
       System.Web.UI.WebControls.WebControl
       From any other control
   Base class for all controls
   Doesn’t implement a user interface
   Use this when you are creating nonvisual
   Derives from System.Web.UI.Control
   Adds User Interface features for handling
   Most controls will derive from WebControl
All other controls
   Used to set UI standards for a development
Design-Time Attributes
   Properties
       Get/Set methods
       Initialize the control
   Attributes
       Display information about properties
       Categorize properties
   Unique identifier of a control
   Must be assigned to a control for the run-
    time methods to work
Run-Time Methods
   CreateChildControls
       Signals a composite control to create child controls
   Dispose
       Enables a controls to perform cleanup
   LoadViewState
       Customized state information restoration
   SaveViewState
       Customized state information persistence
   Render
       Send content to a browser
   Ultimate goal of a control is to display HTML
    in a browser
   ASP.NET runtime will traverse all the controls
    in an ASPX page and call the render method
    on each one
   Place the HTML that will be send in to the
    browser in the Render method
Generate HTML in Render
   Generate raw HTML using the Write method
    of the HtmlTextWriter
   Use the HtmlTextWriter helper functions
       RenderBeginTag
       RenderEndTag
       AddAttribute (prior to RenderBeginTag)
       AddStyleAttribute (prior to RenderBeginTag)
   Client JavaScript can be rendered in the
    same way
Respond To Client Post
   Must Implement
       LoadPostData
           Updates the state of the control as a result of a
           Returns true if the state has changed, otherwise false
       RaisePostDataChangedEvent
           Will be called if LoadPostData returned true
Capture Postback Events
   Implement IPostbackEventHandler
       RaisePostBackEvent
         Will be called when a postback event is caused

         e.g. When a button is clicked

   By default only the button and image button cause a
       Can add a postback method to a control by using some
        simple client side JavaScript, the Render method and the
        GetPostBackEventReference method
       Generating Client-Side Script for Postback (MSDN)
Maintaining State
   Each server control has a ViewState property
   Allows it to participate in state management
   Can be used instead of a private field
   User Controls
   Server Controls

To top