Sunday, September 19, 2004

Creating Custom ASP.NET Components with Nick Hodges

Nick Hodges of Lemanix (The #1 Borland Partner in Minnesota) is covering ASP.NET component building. He will be using Diamondback and Delphi.

First a couple panoramic shots:

This one is from the back of the room. On the left you can see the boom camera they used in the keynotes and general sessions. On the right is one of the many stationary cameras.

This one is from up front where I ended up sitting. Notice the tables they had set up this year is most all the halls. It made taking notes very nice.

UPDATE: These photos are from Nick's ASP.NET Development Strategies, not Creating Custom ASP.NET components. Same speaker, different location and time. This pictures don't do Nick justice. He looked like a rock star up there. When he finished his session they turned on the lights and the lightening video behind him.

Topics to be covered:

  • Why build controls?
  • User Controls
  • Rendering
  • Web Controls
  • Persistence in Properties
  • Block Types
    • Div
    • Span
    • P (Paragraph)
  • Postback Management
  • Lifecycle of a control
  • Client-side Javascripting
  • Composite Controls
    • A control made of multiple controls
  • Complex Properties
  • Inner Properties
  • State Management of Complex Properties
  • Template Controls
  • HTTP Handlers

User Controls

User Controls are "page chunks" that just provide a portion of the page. Typically they contain common headers, footers, etc. Need to have the "*.ascx" extension. Behind the HTML there is also code-behind code.

Server Controls

They abstract the browser so the correct code is rendered for the current client. They encapsulate features and behavior and maintain state. Server Controls are configurable at design-time like components. Server controls are easier to share then user controls.

System.Web.UI.Control is the base class to descend from. Must have a parameter-less constructor. All properties must have get_ and set_ assessors.

Any HTML you want to output must be outputted in the Render method. Simply place the control in an assembly (or package) and install it into the IDE. It is worth noting that the control does not by default have any style support.

For a web control you override RenderContents method. The Render method handles the block tags (P, SPAN, DIV, etc.)

Composite Controls

Composite controls are controls that contain multiple child-controls. This is different then User Controls because all the controls are generated in code with no visual design. Must implement INamingContainer (with no methods) to indicate that it will name all child controls based on the name of the parent control to prevent collision with other controls on the page. All assessors must call EnsureChildContols before accessing the properties of the child controls. Composite controls must override render for customer rendering. If Render is not overridden then the child controls are streamed out one after the other. When overriding Render you can call AddAttributesToRender passing the Writer to automatically render the attributes of the parent control.

The Writer is an HTMLTextWriter class to facilitate the construction of HTML. Internally it uses StringBuilders and understands the concepts of opening and closing tags by calling RenderBeginTag and RenderEndTag. If you want to add attributes then you call AddAttributes before you call RenderBeginTag. To Render a child control call the RenderControl method of the child control, passing in the writer.

No comments: