Extending the ASP.NET TextField User Control - Developing your own flexible NumericTextbox control: the beginning
(Page 2 of 4 )
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 "uctNumericTextField" 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="uctNumericTextField.ascx.vb" Inherits="uctNumericTextField" %>
<%@ Register Src="uctTextField.ascx" TagName="uctTextField" TagPrefix="uc1" %>
<asp:Panel ID="pnlControl" runat="server"><uc1:uctTextField ID="utxtControl" runat="server" />
<asp:RegularExpressionValidator ID="regExpValidator" runat="server" ControlToValidate="utxtControl"
Display="None" ErrorMessage="[*name*] must be valid." ValidationExpression="^d+(?:.d{0,2})?$"></asp:RegularExpressionValidator>
<asp:CustomValidator ControlToValidate="utxtControl" ID="cstmValidator" runat="server" Display="None" ErrorMessage="Value not in range"></asp:CustomValidator></asp:Panel>
<asp:Label ID="lblInfo" runat="server" Text="TextField Numeric"></asp:Label>
According to the above source, you can understand that the entire control is again a part of the Panel control. Within the panel, I created two validators (a Regular Expression validator and a Custom Validator), uctTextField and a Label. Make sure that the path of "uctTextField.ascx" is set properly. The layout should look like the following in design mode (Fig 01):

Developing your own flexible NumericTextbox control: the skeleton
Once the design is completed, we need to start coding. In the code, I followed the following skeleton:
Imports System.ComponentModel
<ControlValueProperty("Text"), _
ValidationProperty("Text")> _
Partial Class uctNumericTextField
Inherits System.Web.UI.UserControl
.
.
#Region "Page Events"
.
.
Public Overrides Sub Focus()
Private Sub ApplyRegExpValString()
Private Property RegExpValString() As String
Public Property IncludeThousandsSeparator() As Boolean
Public Property DecimalPlaces() As Integer
Public Property MaxLength() As Integer
Public Property MaxValue() As Decimal
Public Property MinValue() As Decimal
Public Property Text() As String
Private Function UnFormattedValue(ByVal strValue As String) As String
Private Function FormattedValue(ByVal strValue As String) As String
Public Property ErrorText() As String
Public Property Editable() As Boolean
Public Property Required() As Boolean
Public Property LabelTextValue() As String
Public Property LabelTextAlign() As
Public Property LabelTextVisible() As Boolean
Public Property LabelTextWidth() As System.Web.UI.WebControls.Unit
Public Property AutoPostBack() As Boolean
Public Property CssClassTextBoxControl() As String
Public Property CssClassLabelControl() As String
Public Property CssClassLabelText() As String
Public Property Tooltip() As String
Public Property ValidationGroup() As String
.
.
End Class
I removed the code for clarity. Based on the discussion of the requirements in previous sections, I came up with the above list of properties, methods and events. I removed the TextChanged event from this control. If you need it, you can add it as explained in my previous article.
You could also observe that a few of the properties in the previous user control (uctTextField) also got repeated here. That is necessary as we need to map the same existing functionality of uctTextField to this control. You can modify the properties according to your naming conventions and standards. My intention is just to give you an idea of the list of features of the user control.
Next: Developing your own flexible NumericTextbox control: coding events >>
More ASP.NET Articles
More By Jagadish Chaterjee