Patrick Altman

I am the VP of Engineering at Eldarion, Co-Founder of Amino Software, and a Mentor at jumpstart foundry.

I am equally passionate about writing code as I am about building businesses.

You really should

Posted On

Aug. 20, 2006, 6:37 p.m.

Tags

Do you have an iOS or web application that you'd like help with?

Eldarion offers lean coaching, software development, code reviews, and in some cases staff augmentation.

Give me a call at 615-300-2930 or send us an email and let's see how we can help you.

This site is hosted on

You should consider it for your Django/Python web hosting as well!

How to Build a Custom WebControl using CompositeControl

Yesterday, I posted about a new Email WebControl I just published with promise of more follow up posts about it. You can probably find some of the same pointers outlined in this post elsewhere, and likely written better and more authoritatively than my attempt at it. However, I'll add to blogosphere my own version of "how I wrote a custom WebControl library".

First of all, you'll quickly note that the code below is nothing magical, in fact it could very likely be refactored into something far more elegant and extensible. If you have suggestions, criticisms, or comments, please let me know -- I welcome them all.

Since what I was wanting to accomplish didn't involve any great complex HTML, JavaScript, or CSS wizardry, I decided to just compose a new control as a composite of existing form elements (e.g. Labels, TextBoxes, a Button, and a Validator).

I cheated and used Reflector to see how Microsoft did the same thing with the new Login controls. I then stripped it down to the bare necessities -- the elements themselves and some basic event handling. You'll find no fancy templates or containers in this example.

CompositeControl
First thing to note is that I subclass from (the new in 2.0) CompositeControl. Scott Guthrie highlights some good points about the CompositeControl that I wish I would have seen earlier (it would have saved me some "figure it out time"). CompositeControl gives me a lot of base class functionality (much of which I haven't spent the time to learn yet -- this should demonstrate 1) how easy it is to create your own control library, and 2) the power and elegance of proper API and OO design in the Framework.

Properties
The properties on a WebControl maintain state outside of the object themselves. Remember that the Web tier is basically a stateless platform. Things like ViewState, Cookies, Databases, XML, etc., have been leverages and/or developed to give the Web tier stateful options.

Unlike normal Properties on a class that would store the data in a private variable, it really doesn't make much since to do so here because the object's lifetime won't carry over from request to request without some form of persistence.

So, rather than serializing the entire object and deserializing, we just store and retrieve values from the ViewState. These Properties can be set at design time in the markup tag, or in the PropertyGrid when the composite control is selected.

CreateChildControls()
This is where all the magic happens and the composite is built. I prefer the flexibility in laying out my web documents with DIV elements instead of TABLE structures. I believe this gives more control and flexibility by being able to do most, if not all design work in CSS. That stated, you can see that I wrap with several layers of nesting the controls with DIV containers.

submit_Click()
Last, but certainly not least, is the EventHandler for the ButtonClick event that is raised when the user clicks the control's button. This builds an SMTP object, constructs the message, and sends it. This further saves the user the trouble of writing EventHandler code to deal with this fairly common pattern of email transmission in code.

I hope that this was someway useful to you. I still plan on putting together another "walk-through" on how to consume this control and make use of it in your ASP.NET applications in the next several days or so.

Stay tuned, or better yet, hit the RSS link on the menu bar to get a subscription so you don't miss anything!

Feedback and Commentary

blog comments powered by Disqus