Before yesterday, I didn’t know what I was missing. I’d always gained my knowledge about .NET (and any other technology for that matter) from conferences, classes, reading, work projects, or trying things out on my own. But the ASP.NET custom web controls talk I heard yesterday was a real eye-opener. The speaker, Miguel Castro, made great use of the code snippet feature of VS.NET 2005 to speed up the development he did during his talk. Stepping through actual code in the IDE and not just reading from the few slides he did have made it a very engaging presentation. The only thing that might have made it better was if an electronic version of his presentation was made available after the talk. Until that happens, I’ll record the few notes I captured here.
Personal Notes from Presentation
- WebControls are ultimately “code generators”
- They are server-based components; classes that are OOP-aware
- The terms WebControl and ServerControl are interchangeable
- WebControls are code-only, UserControls are not.
Advantages of WebControls
- Isolation of visual components
- Fully object-oriented
- Browser independent
- Promote declarative programming
- Handle their own state
- direct output to HTML
- least reusable
- very manual
- code is easy to follow
- handle complexity well
- most commonly used
* Inherited controls inherit from rendered and/or composite controls
* Rendered controls usually have direct HTML counterparts.
* HTML controls are a visual designer representation of actual HTML.
When to Use WebControls
- Use inherited controls when merely extending existing functionality will solve a problem.
- Use rendered controls for simple widgets.
- Use composite controls in all other instances.
* The ToolboxData attribute and inheritance make a class a WebControl.
* Override the Render method to generate HTML on-the-fly (rendered controls).
* Advice: use your objects before you develop them (a nod to test-driven development).
* Override CreateChildControls (composite controls).
* ViewState identifies controls with ids, so set them first.
* Tables cannot appear next to each other because they are block elements. Block elements cannot appear inside inline elements. Inline elements can appear next to each other.
* The default master tag for a WebControl is . You should override the TagKey property in .NET 2.0 to change this. In .NET 1.1, you would extend the constructor to do this.
* Implement INamingContainer to make sure control names are unique (.NET 1.1).
- Use ViewState instead of memeber variables when creating properties for WebControls.
- Category, Description, & DefaultValue are basic design-time attributes of every WebControl property.
- Property-mapping needed if a user can change the value of a property.
- Use “this.ChildControlsCreated = false” in your “setters” for visual properties so control changes are reflected right away, without having to close and re-open the page.
- Add event handlers for composite controls using your control constructor. Also declare a public event handler delegate.
- Events are not inheritable
- Make button controls overridable with protected virtual void methods.
- Object properties can be made read-only because their internal properties have their own getters and setters
- Save WebControl state by using an object array. Reserve the zeroth array element for the base.SaveViewState call.
- Style isn’t serializable, so it’s handled slightly differently.
- Use ControlState to keep your control functioning even with ViewState turned off.
Essential ASP.NET With Examples in C#
Essential ASP.NET with Examples in Visual Basic .NET
Building ASP.NET Server Controls
* Just reading good source code is great education