Server Control Tips Tricks by theslasher


									Server Control
Tips & Tricks
Presented by:

Patrick Hynds
Microsoft Regional Director
CriticalSites CTO
•   Brief Overview
•   Render & Control Lifecycle
•   Adding Confirm to a Button
•   Scrollable Datagrid
•   Composite Controls
•   Dynamic Template Loading
•   Debugging Controls
    What Is An ASP.NET Control?
•   Defined:
    – Server-side Web component
    – … but not limited to UI
•   A server control is a class
    – Exposes properties, methods, events
    – Logic is encapsulated

•   Derived from
    –   System.Web.UI.Control or
    –   System.Web.UI.WebControls.WebControl
         Tag-Based Persistence
 • Tags parsed at runtime
    – Create control instance
    – Set initial property values
          ID of the the liststyle property on l1
              Sets instance
Namespace Class to create Marks tag as a server control
<acme:List id=l1 liststyle=number runat=server>
     <acme:ListItem>first item</acme:ListItem>
     <acme:ListItem>second item</acme:ListItem>
     <acme:ListItem>third item</acme:ListItem>
              Sets the Items property on l1
  Page/Control Processing
                First load
                Each control instantiated, set to
     Init       initial state, added to control tree
    Load        User code runs, tests !IsPostBack
                condition to data bind 1st values
 PreRender      CreateChildControls() ensures
                controls are ready to render(if
                Controls save current state
SaveViewState   different than initial values)
   Render       Each control renders itself to the
   Dispose      Pages and all controls are
 Page/Control Processing
                  Post back
                   Last state of the controls is
LoadViewState      restored from ViewState values
 Postback data     Posted data (in the HTTP form) is
                   Events areits associated control
                   passed to fired for controls in
Postback events    tree order, except the event that
                   caused the post. It’s fired last.
    Render & Control Lifecycle
• Problem
  – How do you output UI

• Render is the dominant method
  – Generated output of the control
  – Override to control output

• Control Tree in Page
  – Everything is a control (including Page)
  – Control tree built and walked to output
Control Rendering
Simple Control, EventLog
         Confirm/Alert Button
• Problem
  – Confirm user actions
  – Instruct with expected behavior

• JavaScript pop-up dialog
  – Confirm user action
  – Alert user to information

• Derive from Button
  – Add Properties for confirm/alert
  – Use Attributes property
Confirm/Alert Button
    Derive from Base Controls
• Problem
  – Existing functionality already built
  – Don‟t want to „re-invent the wheel‟

• Derive from ASP.NET server controls
  – Add functionality or behavior
  – Add new properties

• Override Render
  – Base.Render
  – Render out derived control
• Problem
  – Lots of data in DataGrid
  – Don‟t want to scroll browser

• ScrollableDataGrid control
  – Composite control that wraps DataGrid
  – Simply adds <div></div> tag

• Great candidate for a server control
  – Cleaner HTML
  – No messy <div> tags in design code
Scrollable DataGrid
         Composite Controls
• Problem
  – How to build more complex UI interaction
  – Render is possible, but…

• A Composite Control is
  – A control containing other controls
  – Act together to solve a common problem

• A Composite control must
  – Implement INamingContainer
  – Overrides CreateChildControls()
Composite Control
   Dynamic Template Loading
• Problem
  – Choose template based on conditions
  – Static definition is easy

• Templates can be dynamically loaded
  – Page.LoadTemplate(string template)
  – Template is an *.ascx file

• Databinding
  – Must explicitly set Container data type
  – Templates must all be in separate files
Dynamic Template
          Debugging Controls
•   Problem
    – How to debug server controls
    – Cannot just „run‟ project

•   Steps
    1. Build control project in debug mode
    2. Set breakpoint(s)
    3. In Visual Studio .NET: Debug | Process
Debugging Controls
        A look into the future
• VS .Net 2005 will provide more than 70
  new Server Controls
• Partners will (and already do) provide lots
  of controls that can be used in the same
  way as the standard controls
• PAG group is now providing details on how
  to do complex tasks well via their “Blue

• Server Controls can be very useful
• Not a technology without its complexities
• Read MS Press book by Nikhil Kothari
Please fill out your session evaluations!

     Server Control Tips & Tricks

    Patrick J. Hynds
    CTO, CriticalSites, Boston and New York
    Microsoft Regional Director for Boston

To top