Problem registering startup script in a Ajax Validator

D

Dhanashree

I have a RangeValidator which uses AJAX to call server-side validation
function from the client. The code works fine, but I have one problem.

following is my range validator class:

public class RangeValidator: BaseValidator,ICallbackEventHandler
{
public event ServerValidateEventHandler ServerValidate;
String _controlToValidateValue;

protected override void OnPreRender(EventArgs e)
{
String eventRef =
Page.ClientScript.GetCallbackEventReference(this, "", "", "");

// Register include file
String includeScript = Page.ResolveClientUrl("~/
ClientScripts/AjaxValidator.js");

Page.ClientScript.RegisterClientScriptInclude("MyAjaxValidator",
includeScript);

// Register startup script
String startupScript =
String.Format("document.getElementById('{0}').evaluationfunction =
'AjaxValidatorEvaluateIsValid';", this.ClientID);
Page.ClientScript.RegisterStartupScript(this.GetType(),
this.ClientID, startupScript);

base.OnPreRender(e);
}
.....
}

This is how I use it in the aspx file

<cc1:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="Test1"
Display="Dynamic" OnServerValidate="test1_servervalidate"></
cc1:RangeValidator>


<cc1:RangeValidator ID="RangeValidator2" runat="server"
ControlToValidate="Test2"
Display="Dynamic" OnServerValidate="test2_servervalidate"></
cc1:RangeValidator>

So the onPreRender is called twice when this page loads. The code
compiles and the validation works fine, but only for the first "Test1"
control only. And it doesnt fire the validation events for the second
one.

Here is how it is rendered:

<script type="text/javascript">
<!--
WebForm_InitCallback();document.getElementById('RangeValidator1').evaluationfunction
= 'AjaxValidatorEvaluateIsValid';// -->
</script>

<script type="text/javascript">
<!--
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
ValidatorOnLoad();
}

function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else {
return true;
}
}
// -->
</script>

<script type="text/javascript">
<!--
document.getElementById('RangeValidator2').evaluationfunction =
'AjaxValidatorEvaluateIsValid';// -->
</script>

Any idea on why this is not working for the second validator? Thanks
in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top