Hi,
I have a UserControl called NumericBox which is simply a UserControl with a single TextBox:
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.asp Code:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="NumericBox.ascx.vb" Inherits="F1TimeTrials.Controls.NumericBox" %> <asp:TextBox runat="server" ID="txt" />
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:
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'.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
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!


Reply With Quote
