Developing a Wonderful ASP.NET TextField Control - Developing your own flexible Textbox control with new features: identifying requirements
(Page 2 of 4 )
In this section, I would like to introduce a new textbox control called "uctTextField." The "uct" stands for "User Control" as part of my naming convention. Let us consider the features not available with the existing Textbox server control (which is available as part of the ASP.NET framework).
- It doesn't support labeling. You may have to add another label control to support the existing Textbox control.
- To maintain some gap between Label and Textbox, you may have to work with TABLEs in HTML.
- It doesn't have the "required input" option. You may have to add RequiredFieldValidator to the same Textbox control separately.
- It doesn't have support for hints. Hints (or messages of format, etc.) are generally used when inputting dates and other items. You may have to add another Label control to support the same.
- If I disable the Textbox, it simply locks the user out of the Textbox. But it doesn't automatically convert to Label (when in disabled mode).
At the moment, I feel that these features are frequently used in every application. Now imagine what would happen if I have five textboxes on the web page. I have to create five labels, validators, hint labels, and read-only labels, working with layout and more. You may want to be able to implement your own features instead of (or in addition to) the ones I've described here. This scenario is always boring for any ASP.NET developer.
To solve these "boring" problems, to a certain extent (or even to the full extent sometimes), user controls would be a great help. Now, I would like to design and develop a user control (called "uctTextField") supporting all of the above features. Once the control is developed, it can be dragged and dropped onto the web page any number of times, and it really improves my productivity.
Developing your own flexible Textbox control with new features: the beginning
Once you know the requirements for creating a new user control, it is time to implement the user control as part of your ASP.NET web solution. Using the Solution Explorer, right click on your solution and choose Add New Item. Within the Add New Item dialog box, select Web User Control as the template, provide the name of the User Control as "uctTextField" and hit Add.
Drag and drop the controls (and other stuff) onto your user control, so that it looks likes the following in the source mode:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="uctTextField.ascx.vb" Inherits="uctTextField" %>
<asp:Panel ID="pnlControl" runat="server">
<table cellpadding="0" cellspacing="0" border="0">
<tr valign="top">
<td style="white-space: nowrap">
<asp:Panel ID="pnlText" runat="server" Visible="false"
HorizontalAlign="Right">
<asp:Label ID="lblText" runat="server" >Label:</asp:Label>
</asp:Panel>
</td>
<td>
<asp:Label ID="lblControl" runat="server" Width=""
Visible="false"></asp:Label>
<asp:TextBox ID="txtControl" runat="server"></asp:TextBox>
<span id="spnRequiredIndicator" runat="server"
class="requiredastrisk" visible="false">*</span>
<asp:Panel ID="pnlHint" runat="server" Visible="false"
HorizontalAlign="Left">
<asp:Label ID="lblHint" runat="server" ></asp:Label>
</asp:Panel>
</td>
</tr>
</table>
<asp:RequiredFieldValidator ID="reqfValidator" runat="server"
ErrorMessage="[*name*] is required." ControlToValidate="txtControl"
Display="None" Enabled="false"></asp:RequiredFieldValidator>
</asp:Panel>
According to the above source, you can understand that the entire control is a part of the Panel control. Within the panel, I created a simple HTML table to lay out all the controls in a convenient manner. The control mainly contains three Label controls, one Textbox control and one RequiredFieldValidator. For my convenience in layout, I added two more panels within the HTML table. The layout should look like the following in design mode (Fig 01).

Next: Developing your own flexible Textbox control with new features: the skeleton >>
More ASP.NET Articles
More By Jagadish Chaterjee