NumericBox UserControl and validators
Hi,
I have a UserControl called NumericBox which is simply a UserControl with a single TextBox:
asp Code:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="NumericBox.ascx.vb" Inherits="F1TimeTrials.Controls.NumericBox" %>
<asp:TextBox runat="server" ID="txt" />
In the code-behind, I add some attributes to the textbox which handle some events such as OnKeyPress (with some javascript) to make sure the user can only enter numeric values. I don't think the javascript is relevant but I can post it if it is.
This works fine, I can put this control on my page and it will only allow numeric input. The next step is to use two kinds of validator controls: a RequiredFieldValidator and a RangeValidator, to ensure that the user has entered a value at all and that it is in the allowed range.
Both of these don't seem to work... The first problem was that my UserControl is not one of the controls the validators can validate, so I had to add a System.Web.UI.ValidationProperty attribute to my class. As far as I know this should have the name of the property that determines the value of the control, so that's exactly what I did:
vb.net Code:
Namespace Controls
<System.Web.UI.ValidationProperty("Value")> _
Public Class NumericBox
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.SetAttributes()
End Sub
Public ReadOnly Property TextBox As TextBox
Get
Return txt
End Get
End Property
Private _AllowNegative As Boolean = False
Public Property AllowNegative() As Boolean
Get
Return _AllowNegative
End Get
Set(ByVal value As Boolean)
_AllowNegative = value
End Set
End Property
Private _AllowDecimal As Boolean = False
Public Property AllowDecimal() As Boolean
Get
Return _AllowDecimal
End Get
Set(ByVal value As Boolean)
_AllowDecimal = value
End Set
End Property
Public Property Width As Unit
Get
Return txt.Width
End Get
Set(ByVal value As Unit)
txt.Width = value
End Set
End Property
Private Sub SetAttributes()
Dim strDecimal = Me.AllowDecimal.ToString.ToLower
Dim strNegative = Me.AllowNegative.ToString.ToLower
txt.Attributes.Remove("onblur")
txt.Attributes.Remove("onkeyup")
txt.Attributes.Remove("onkeypress")
txt.Attributes.Add("onblur", String.Format("extractNumber(this, 2, {0});", strNegative))
txt.Attributes.Add("onkeyup", String.Format("extractNumber(this, 2, {0});", strNegative))
txt.Attributes.Add("onkeypress", String.Format("return blockNonNumbers(this, event, {0}, {1});", strDecimal, strNegative))
End Sub
Public Property Value As Object
Get
If txt.Text = String.Empty Then Return 0
If Me.AllowDecimal Then Return Double.Parse(txt.Text)
Return Integer.Parse(txt.Text)
End Get
Set(ByVal value As Object)
If Me.AllowDecimal Then
txt.Text = CDbl(value).ToString("0.00")
Else
txt.Text = value.ToString()
End If
End Set
End Property
End Class
End Namespace
The Value property returns either the Double or Integer (based on whether AllowDecimal is true or false) equivalent of the text in the textbox 'txt'.
I let the System.Web.UI.ValidationProperty attribute point to this Value property, and now I can use both validators without warnings in the editor.
However, it does not seem to work during run-time.
Here's an example how I'm using it:
asp Code:
<uc:NumericBox runat="server"
ID="numFrontWingAngle"
AllowDecimal="false"
AllowNegative="false"
Width="30px" />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="numFrontWingAngle"
ErrorMessage="* Cannot be empty."
ForeColor="Red" />
<asp:RangeValidator runat="server"
ControlToValidate="numFrontWingAngle"
MinimumValue="1"
MaximumValue="11"
ErrorMessage="* Range is 1-11."
ForeColor="Red"
Type="Integer" />
It doesn't work. I can enter values outside the range 1-11, I can even leave it empty, the validators never show their warnings or stop the page from posting back (as they should, right?)
What is causing my problem? I thought the RequiredFieldValidator might not work because the Value is returned as 0 if the textbox is empty, but even then, 0 is outside the range 1-11 so at least the RangeValidator should complain?
Thanks!
Re: NumericBox UserControl and validators
Hello Nick,
I think you might be over complicating this a bit...
Why not put the Validators in the UserControl, and let them validate the simple TextBox?
You could then extend the UserControl to expose properties, which could either be set at runtime or design time, which would then set the properties of the validators, i.e. the upper and lower limit etc.
Gary
Re: NumericBox UserControl and validators
I suppose I could do that. I figured it would be easier for now just to put the validators in the page itself, but if there's no simple solution to this then I will put them in the UserControl. I'm sure there must be a solution though?
Re: NumericBox UserControl and validators
Hey,
I am sure there is a solution as well, but I didn't really put much thought into it.
At the end of the day, your control should be a self contained entity. If you drag one onto your page you want it just to work, with your configuration, you don't want to have to be adding other controls onto the page. This goes against the grain.
Gary