ASP:RequiredTextBox and ASP:HoverTextBox Controls
9/17/2007 8:05:14 PM
Andrew Robinson gave a great presentation at our monthly .NET User Group meeting last Wednesday night on extending ASP.NET Web Controls (System.Web.UI.WebControls). Driving home from the meeting I started thinking of how I could extend controls for my benefit. The first arena was that of input validation for web forms. Adding input fields (textboxes) to a form and then having to associate a RequiredFieldValidator for said textbox gets real old, real fast. Think of a form with 20 fields. That's 20 RequiredFieldValidators. That's 20 of this:
<
asp:TextBox ID="textEmailAddress" runat="server" /><asp:RequiredFieldValidator ID="requiredEmailAddress" ControlToValidate="textEmailAddress" ErrorMessage="Required!" runat="server" />
Doesn't look too fun does it? I thought of creating a control that had the option of being a required field and if so, specifying a message to display. So now all I have to do is type this:
<
asp:RequiredTextBox ID="textEmailAddress" IsRequired="true" ErrorMessage="Required!" runat="server" />
By default this control has EnableClientScript set to "true" for DHTML / client-side validation (as does the RequiredFieldValidator if I'm not mistaken). If you want to validate on the server you can simply type EnableClientScript="false". Since a required field validator is created I also added a property for ValidationGroup (which are a nice addition to 2.0).
<
asp:RequiredTextBox ID="textEmailAddress" IsRequired="true" ErrorMessage="Required!" ValidationGroup="signup" runat="server" />
Also if I make a field required but don't specify an ErrorMessage it defaults to "*". I can now cut the amount of code / typing down to:
<
asp:RequiredTextBox ID="textEmailAddress" IsRequired="true" runat="server" />
This took me about an hour to create and it was worth every second. Here is a limited functionality version posted on The Code Project that I used for a base.
HoverTextBox
The next control I thought to create was what I call a HoverTextBox. This is a very simple controls and is used to pretty-up a UI (if that's your thing!). I did contract work for a firm awhile back that was big on creating nice UI's. This control allows you to select an onMouseOver (focus())color as well as an onMouseOut (blur()) color. With this control you can give you end-user a visual representation of where they are on a large form.
<
asp:HoveTextBox ID="textAddress1" OnMouseOverColor="Aqua" OnMouseOutColor="White" Default="White" runat="server" />
Now you don't have to create CSS to get this effect. Oh, I forgot to mention that you can set a default color. So all textboxes will render to "Wheat" if that is the color you selected. Again, no more CSS (at least for this part of the textbox behavior!).

I plan on giving this away on my site in the near future as well possibly posting the source. I don't want to do that just yet because my "Control Library" only has these 2 two controls so far. Next I'm working on adding Phone Number (US), SSN, IP TextModes to the TextBox. These will include embedded javascript for on-the-fly formatting of text input / rendering.
If you're wondering how I got the asp prefix it's as easy as registering the "asp" prefix.
<%@ Register TagPrefix="asp" Namespace="ControlExtensionLibrary" Assembly="ControlExtensionLibrary" %>
Anyone else ever work on extending / creating controls?
.NET,
C#
