troubleshooting Question

Help on extending the compare validator.

Avatar of kaytrim
kaytrim asked on
.NET Programming
25 Comments1 Solution1974 ViewsLast Modified:
The problem that I am having is the prompt box doesn't apear from the JavaScript function if the value in the text box doesn't match, just the text "not a match" apears. Can anyone give me some help please? It dosen't look like my JavaScript function is being called at all as I put an alert message at the start of the fucntion and it didn't apear either. The JavaScript is being added to the page as I looked a the source of the page after it was rendered in the browser.

A little background on the project. We are trying to create a medical study web entry system that uses double data entry. As we could be doing this many more times in the future we decided to create some custom controls to help us with the validation. The way that the validation is slated to work is like this.

1. First user enters the data from a paper form.

2. Second user enters unique id from the same paper form.
2a. I get the data from the database for the first entry.
2b. the data is used for the 'valueToCompare' properties of the validation controls, these values are not shown to the user.

3. Second user enters rest of the form.
3a. if the value that the second user entered doesn't match the value from the first entry the second user is notified and a judgment call is made to what the value should be.

4. data is saved to the database.

Here is the class that I created for a custom compare validation control is done with VB.NET:
---------------------------------------------------------------------------------------
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text
Imports Microsoft.VisualBasic

Namespace DoubleDataEntryValidators

Public Class DDECompareValidator : Inherits CompareValidator
Protected _QuestionSummary As String = "(The Question that you just answered.)"

Public WriteOnly Property QuestionSummary() As Integer
Set(ByVal Value As Integer)
_QuestionSummary = Value
End Set
End Property

Protected Overrides Function ControlPropertiesValid() As Boolean
Return True
End Function

Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
If Me.EnableClientScript Then
Me.ClientScript()
End If
MyBase.OnPreRender(e)
End Sub

Protected Sub ClientScript()
Me.Attributes("evaluatefunction") = "dde_CompareValidatorEvaluateIsValid"
Dim sb_Script As New StringBuilder()
Dim CR As New Char()
Dim DQ As New Char()
CR = Chr(13) ' Chr(13) returns a carrage return character
DQ = Chr(34) ' Chr(34) returns a double quote character

sb_Script.Append("<script language=" & DQ & "javascript" & DQ & ">")
sb_Script.Append(CR)
sb_Script.Append(CR)
sb_Script.Append("function dde_CompareValidatorEvaluateIsValid(val) {")
sb_Script.Append(CR)
sb_Script.Append(" var value = ValidatorGetValue(val.controltovalidate);")
sb_Script.Append(CR)
sb_Script.Append(" var correctValue;")
sb_Script.Append(CR)
sb_Script.Append(" var questionString;")
sb_Script.Append(CR)
sb_Script.Append(" if (ValidatorTrim(value).length == 0) {")
sb_Script.Append(CR)
sb_Script.Append(" return true;}")
sb_Script.Append(CR)
sb_Script.Append(" var compareTo = " & DQ & DQ & ";")
sb_Script.Append(CR)
sb_Script.Append(" if (null == document.all[val.controltocompare]) {")
sb_Script.Append(CR)
sb_Script.Append(" if (typeof(val.valuetocompare) == " & DQ & "string" & DQ & ") {")
sb_Script.Append(CR)
sb_Script.Append(" compareTo = val.valuetocompare;")
sb_Script.Append(CR)
sb_Script.Append(" }")
sb_Script.Append(CR)
sb_Script.Append(" }")
sb_Script.Append(CR)
sb_Script.Append(" else {")
sb_Script.Append(CR)
sb_Script.Append(" compareTo = ValidatorGetValue(val.controltocompare);")
sb_Script.Append(CR)
sb_Script.Append(" questionString = " & DQ & "Question: " & DQ & " + val.questionsummary + " & DQ & " \r\nThe first entry is: " & DQ & " + value + " & DQ & " The second entry is: " & DQ & " + compareTo + " & DQ & " \r\nPlease enter the correct value." & DQ & ";")
sb_Script.Append(CR)
sb_Script.Append(" window.alert(questionString)")
sb_Script.Append(CR)
sb_Script.Append(" if (ValidatorCompare(value, compareTo, val.operator, val)) {")
sb_Script.Append(CR)
sb_Script.Append(" }")
sb_Script.Append(CR)
sb_Script.Append(" else {")
sb_Script.Append(CR)
sb_Script.Append(" correctValue = window.prompt(questionString, " & DQ & DQ & ");")
sb_Script.Append(CR)
sb_Script.Append(" if(correctValue != null) {")
sb_Script.Append(CR)
sb_Script.Append(" val.controltovalidate.value = correctValue;")
sb_Script.Append(CR)
sb_Script.Append(" }")
sb_Script.Append(CR)
sb_Script.Append(" }")
sb_Script.Append(CR)
sb_Script.Append(" }")
sb_Script.Append(CR)
sb_Script.Append("}")
sb_Script.Append(CR)
sb_Script.Append("</script>")
sb_Script.Append(CR)

Me.Page.RegisterClientScriptBlock("DDEScript", sb_Script.ToString())
End Sub
End Class

End Namespace
---------------------------------------------------------------------------------------

This class is complied and the resulting dll is placed in the bin directory. I know that it may be a little hard to follow the ClientScript sub so here is the resulting JavaScript function, it calls other functions in the standard javascript file
"/intepub/wwwroot/aspnet_client/system_web/1_0_3705_0/WebUIValidation.js"

---------------------------------------------------------------------------------------
function dde_CompareValidatorEvaluateIsValid(val) {
var value = ValidatorGetValue(val.controltovalidate);
var correctValue;
var questionString;
if (ValidatorTrim(value).length == 0) {
return true;}
var compareTo = "";
if (null == document.all[val.controltocompare]) {
if (typeof(val.valuetocompare) == "string") {
compareTo = val.valuetocompare;
}
}
else {
compareTo = ValidatorGetValue(val.controltocompare);
questionString = "Question: " + val.questionsummary + " \r\nThe first entry is: " + value + " The second entry is: " + compareTo + " \r\nPlease enter the correct value.";
window.alert(questionString)
if (ValidatorCompare(value, compareTo, val.operator, val)) {
}
else {
correctValue = window.prompt(questionString, "");
if(correctValue != null) {
val.controltovalidate.value = correctValue;
}
}
}
}

---------------------------------------------------------------------------------------

Now here is the code for the aspx page.

---------------------------------------------------------------------------------------
<%@Register TagPrefix="mcv" Namespace="DoubleDataEntryValidators" Assembly="MyValidator" %>
<%@Page Language="vb" AutoEventWireup="false" Codebehind="TestValidator.aspx.vb" Inherits="test.TestValidator" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>TestValidator</title>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server">

<asp:TextBox id=TextBox1 runat="server"></asp:TextBox>
<mcv:DDECompareValidator runat="server" ValueToCompare="123" ControlToValidate="TextBox1" id=mcv2>not a match</mcv:DDECompareValidator>

</form>

</body>
</HTML>
---------------------------------------------------------------------------------------

Notice that the class is registered in the first line of the html and the tag "<mcv:DDECompareValidator" compares the value "123" to what ever is in TextBox1.


Michael
 

--------------------------------------------------------------------------------
"We either make ourselves miserable,
or we make ourselves strong.
The amount of work is the same."
Carlos Castaneda
ASKER CERTIFIED SOLUTION
CJ_S

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 25 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 25 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros