Newbie mistake or Custom Validator bug?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a very strange problem involving controls and the CustomValidator.

We have created a custom control. The control includes an ordinary textbox,
a customvalidator with the ControlToValidate set to the textbox, and a
VBScript validator subroutine. The purpose of the sub is to insure that only
dates that fall on Monday are permitted. The custom validator is exposed
through a property to permit enabling and disabling as needed.

The problem is this. If I have two instances of the control on a page, the
custom validator of the first instance is apparently calling the sub of the
second. The net effect is that I cannot properly validate Mondays on the
first control. This control seems to work fine when there is only one
instance on a page. When I examine the HTML source, it appears that there
are the correct instances of the code, each accessing the correct controls.

(I should admit upfront that the control also contains a ThirdParty calendar
date selector, Telerik, but as far as I can tell, none of the problem code
interacts with it in any way. It's sole purpose is to put a property
formatted date string into the textbox.)
 
I have just created a test case (without any 3rd party controls,
incidentally. That appears to have nothing to do with the problem)

First create a user control with a textbox and a custom validator:
----- ASPX starts here ---------------
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="CalendarTest.ascx.vb" Inherits="test1.CalendarTest"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:TextBox id="txtDate" runat="server" Columns="18"
CssClass="text"></asp:TextBox>
<asp:CustomValidator id="MondayValidator1" runat="server"
ClientValidationFunction="ValidateMondayVB"
ErrorMessage="The Date is not a Monday" Enabled="False"
ControlToValidate="txtDate">*</asp:CustomValidator>
<script language="vbscript">

Sub ValidateMondayVB(source, args)

If IsDate(document.forms("Form1").item("<%= Me.txtDate.ClientID
%>").Value) then

startDt = CDate(document.forms("Form1").item("<%= Me.txtDate.ClientID
%>").Value)
msgbox "Here I am control <%= Me.ClientID %>, box = <%=
Me.txtDate.ClientID %> , source.id = " & source.id
If WeekDay(startDt) <> vbMonday then
msgbox "Start date must be a Monday. You specified a " &
WeekdayName(WeekDay(startDt)) & "."
args.IsValid = false
Else
args.IsValid = true
End if

End if

End Sub
</script>

------- ASPX ends here ------------
Next add the following property to the control code behind:
Public Property MondayValidator() As CustomValidator
Get
Return MondayValidator1
End Get
Set(ByVal Value As CustomValidator)
MondayValidator1 = Value
End Set
End Property
--------
Now create a test page using the control:

------- test page ASPX starts here ---------------------------------

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm12.aspx.vb" Inherits="test1.WebForm12"%>
<%@ Register TagPrefix="uc1" TagName="CalendarTest" Src="CalendarTest.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm12</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:ValidationSummary id="ValidationSummary1" runat="server"
BorderStyle="Outset" Width="272px"></asp:ValidationSummary>
<P>
<uc1:CalendarTest id="CalendarTest1"
runat="server"></uc1:CalendarTest>
<uc1:CalendarTest id="CalendarTest2"
runat="server"></uc1:CalendarTest></P>
<P>&nbsp;</P>
<P>
<asp:Button id="Button1" runat="server" Text="Validate"></asp:Button></P>
</form>
</body>
</HTML>
-------- test page ASPX ends here ---------------------

Finally add the following to the test page's Page_Load

CalendarTest1.MondayValidator.Enabled = True
CalendarTest1.MondayValidator.ErrorMessage = "Test1 Fired"
CalendarTest2.MondayValidator.Enabled = False
CalendarTest2.MondayValidator.ErrorMessage = "Test2 Fired"

Now run the project. Enter a Monday date (2/20/2006 for example) in the
first box. Enter a non-Monday date in the second. (In my actual work, these
boxes would have Javascript formatting routines forcing a mm/dd/yyyy format
so only use that.) Now press the button. The first validator fires after
apparently using the 2nd instances VBscript sub, or so it looks to me.

Thanks
 
Back
Top