Extending the ASP.NET TextField User Control - Developing your own flexible NumericTextbox control: coding events
(Page 3 of 4 )
I am currently using Page Events to simply maintain ViewState. The code is as follows:
#Region "Page Events"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
ErrorText = ViewState.Item("ErrorText").ToString
Required = CType(ViewState.Item("Required"), Boolean)
DecimalPlaces = CType(ViewState.Item("DecimalPlaces"), Integer)
IncludeThousandsSeparator = CType(ViewState.Item("IncludeThousandsSeparator"), Boolean)
RegExpValString = ViewState.Item("RegExp").ToString
MaxValue = CType(ViewState.Item("MaxValue"), Decimal)
MinValue = CType(ViewState.Item("MinValue"), Decimal)
Else
Me.lblInfo.Visible = False
End If
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
ViewState.Item("ErrorText") = ErrorText
ViewState.Item("Required") = Required
ViewState.Item("DecimalPlaces") = DecimalPlaces
ViewState.Item("IncludeThousandsSeparator") = IncludeThousandsSeparator
ViewState.Item("RegExp") = RegExpValString
ViewState.Item("MaxValue") = MaxValue
ViewState.Item("MinValue") = MinValue
End Sub
#End Region
The importance of "lblInfo" is worth mentioning at this moment. When a user control simply contains another user control as part of the design, it doesn't show up properly during design time (on ASPX). All the nested user controls look hidden, and the user would not be happy with an empty control. That's why I added "lblInfo" to show at design time that it is a numeric user control. At runtime it gets automatically hidden.
Apart from the events, I used a Focus method. When the control receives the focus, the cursor should be available in the text box. If the control is in read-only mode, it will not be able to receive any focus. It is defined as follows:
Public Overrides Sub Focus()
If Editable Then Me.utxtControl.Focus()
End Sub
You can observe that I am calling the focus method of "utxtControl" when this control receives the focus. The validation based on Max and Min values is handled as follows:
Protected Sub cstmValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cstmValidator.ServerValidate
Dim value As Decimal = UnFormattedValue(args.Value)
If value > MaxValue Or value < MinValue Then
args.IsValid = False
cstmValidator.ErrorMessage = Me.ErrorText & " should be in between " & Me.MinValue & " and " & Me.MaxValue
Else
args.IsValid = True
End If
End Sub
Developing your own flexible NumericTextbox control: dealing with Regular Expressions
Our control needs to deal with Regular Expressions as we would like to have support for decimal places, commas and so forth. The regular expressions are applied as follows based on the properties set by the user:
Private Sub ApplyRegExpValString()
If IncludeThousandsSeparator Then
If DecimalPlaces > 0 Then
RegExpValString = "(?n:(^$?(?!0,?d)d{1,3}(?=(?<1>,)|(?<1>))(k<1>d{3})*(.d{0," & DecimalPlaces & "})?)$)"
Else
RegExpValString = "(?n:(^$?(?!0,?d)d{1,3}(?=(?<1>,)|(?<1>))(k<1>d{3})*)$)"
End If
Else
If DecimalPlaces > 0 Then
RegExpValString = "^d+(?:.d{0," & DecimalPlaces & "})?$"
Else
RegExpValString = "^d+?$"
End If
End If
End Sub
Private Property RegExpValString() As String
Get
Return m_strRegExp
End Get
Set(ByVal value As String)
m_strRegExp = value
Me.regExpValidator.ValidationExpression = value
End Set
End Property
A few of the regular expressions which are most commonly used for these types of scenarios are as follows
- (?n:(^$?(?!0,?d)d{1,3}(?=(?<1>,)|(?<1>))(k<1>d{3})*(.dd)?)$) - for $,comma,compulsory 2 dig dec
- (?n:(^$?(?!0,?d)d{1,3}(?=(?<1>,)|(?<1>))(k<1>d{3})*)$) - for $, comma, no dig dec
- (?n:(^$?(?!0,?d)d{1,3}(?=(?<1>,)|(?<1>))(k<1>d{3})*(.d{0,2})?)$) - for $, comma, option 2 dig dec
- ^d+(?:.d{0,2})?$ - without $, no comma, optional 2 dig dec
- ^d+?$ - without $, no comma, no dec
Next: Developing your own flexible NumericTextbox control: properties to retrieve values >>
More ASP.NET Articles
More By Jagadish Chaterjee