B
Boltar
Hello,
I am trying to use a CustomValidator control to perform client-side
validation via a server callback.
I have an example below that validates if a textbox contains an odd
number.
I realize that I can use client-side javascript entirely for this, I
have just created a simplified example. My actual callback validation
routine involves accessing a database but I figured a simplified
example would be better.
The callback validation in my code seems to work ok, and returns the
result correctly to the CallBackReturnFunction() javascript function,
however it does not seem to be able to pass the IsValid values
correctly at the end of the function.
Any input would be appreciated.
The codebehind for this page is located here:
http://rainz.org/code/codebehind.txt
Here is the page code:
<%@ Page Language="VB" AutoEventWireup="false"
CompileWith="OddTest.aspx.vb" ClassName="OddTest_aspx" %>
<html>
<head>
<title>Odd Number Callback Validation</title>
<script language="javascript">
function ClientValidate(source, arguments) {
var message = arguments.Value;
// create context variable
var oContext = new Object();
oContext.args = arguments;
<%=sCallBackFunctionInvocation%>
}
function CallBackReturnFunction(sReturnValue, oContext) {
var result='The callback return value is ' +
sReturnValue + '\n';
if (sReturnValue=='true')
{result = result + 'ODD Number, Validation Passed'}
else
{result = result + 'NOT Odd Number, Validation
Failed'}
alert(result);
if (sReturnValue == 'true') { /*validation passed*/
oContext.args.IsValid=true
}
else { /*validation failed*/
oContext.args.IsValid=false
}
}
function OnError(message, oContext) {
alert('An unhandled exception has occurred:\n' + message);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="Message"
Text="Enter an odd number:"
Font-Names="Verdana"
Font-Size="10pt"
runat="server"/>
<asp:TextBox id="Text1"
runat="server" />
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1"
ClientValidationFunction="ClientValidate"
OnServerValidate="ServerValidation"
Display="Static"
ErrorMessage="Not an odd number!"
ForeColor="green"
Font-Names="verdana"
Font-Size="10pt"
runat="server"/>
<p>
Leave blank for testing, prevents server validation, we want
to verify client callback validation:
<asp:TextBox ID="Text2" Runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
Runat="server" ErrorMessage="*required"
ControlToValidate="Text2">
</asp:RequiredFieldValidator>
<br />
<br />
<asp:Button ID="Button1" Runat="server" Text="Button"
OnClick="Button1_Click" />
</p>
</form>
</body>
</html>
I am trying to use a CustomValidator control to perform client-side
validation via a server callback.
I have an example below that validates if a textbox contains an odd
number.
I realize that I can use client-side javascript entirely for this, I
have just created a simplified example. My actual callback validation
routine involves accessing a database but I figured a simplified
example would be better.
The callback validation in my code seems to work ok, and returns the
result correctly to the CallBackReturnFunction() javascript function,
however it does not seem to be able to pass the IsValid values
correctly at the end of the function.
Any input would be appreciated.
The codebehind for this page is located here:
http://rainz.org/code/codebehind.txt
Here is the page code:
<%@ Page Language="VB" AutoEventWireup="false"
CompileWith="OddTest.aspx.vb" ClassName="OddTest_aspx" %>
<html>
<head>
<title>Odd Number Callback Validation</title>
<script language="javascript">
function ClientValidate(source, arguments) {
var message = arguments.Value;
// create context variable
var oContext = new Object();
oContext.args = arguments;
<%=sCallBackFunctionInvocation%>
}
function CallBackReturnFunction(sReturnValue, oContext) {
var result='The callback return value is ' +
sReturnValue + '\n';
if (sReturnValue=='true')
{result = result + 'ODD Number, Validation Passed'}
else
{result = result + 'NOT Odd Number, Validation
Failed'}
alert(result);
if (sReturnValue == 'true') { /*validation passed*/
oContext.args.IsValid=true
}
else { /*validation failed*/
oContext.args.IsValid=false
}
}
function OnError(message, oContext) {
alert('An unhandled exception has occurred:\n' + message);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="Message"
Text="Enter an odd number:"
Font-Names="Verdana"
Font-Size="10pt"
runat="server"/>
<asp:TextBox id="Text1"
runat="server" />
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1"
ClientValidationFunction="ClientValidate"
OnServerValidate="ServerValidation"
Display="Static"
ErrorMessage="Not an odd number!"
ForeColor="green"
Font-Names="verdana"
Font-Size="10pt"
runat="server"/>
<p>
Leave blank for testing, prevents server validation, we want
to verify client callback validation:
<asp:TextBox ID="Text2" Runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
Runat="server" ErrorMessage="*required"
ControlToValidate="Text2">
</asp:RequiredFieldValidator>
<br />
<br />
<asp:Button ID="Button1" Runat="server" Text="Button"
OnClick="Button1_Click" />
</p>
</form>
</body>
</html>