.NET Framework - ASP.NET Validator is removing client onChange event handler for custom server control

Asked By Lewis Holmes on 10-Mar-09 05:49 AM

I have developed a custom server control that basically renders one or more
drop down lists (select html controls) to display a hierarchical lookup
list. So for example you may have  lookup data structure that is made up of
3 levels and each level is displayed as a seperate drop down on my web form.
If the user changes the selected value for one of the drop downs, some
javascript executes which dynamically populates the data for the next level
of the hierarchy. This JavaScript is added to the onChange event of the html
select control (drop down).

So for example you may have 3 levels defined as "Car Manufacturer", "Model"
and "Trim". After selecting a car manufacturer, the models available for
that selected manufacturer are then loaded into the Model drop down using

This all works nicely but then when I add a Compare validator to my web form
dynamically and set it to validate this custom server control, it appears as
if my onChange JavaScript event handler script is being removed. I have been
using IE Developer Toolbar to inspect the DOM and when I dynamically add the
validator, the onChange I already have is being removed. I am 99% sure it is
the presence of the validation control that is removing my onChange

I decided to have a look into the ASP.NET validation JavaScript and I can
see there is a function that hooks into the onchange events for controls to
make sure the validation script runs when a values is changed. I think this
is the JavaScript that is removing my onChange script.

function ValidatorHookupEvent(control, eventType, functionPrefix) {
var ev;
eval("ev = control." + eventType + ";");
if (typeof(ev) == "function") {
ev = ev.toString();
ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
else {
ev = "";
var func;
if (navigator.appName.toLowerCase().indexOf('explorer') > -1) {
func = new Function(functionPrefix + " " + ev);
else {
func = new Function("event", functionPrefix + " " + ev);
eval("control." + eventType + " = func;");

Please could somebody provide me with some help with this. I am not sure why
my onChange script is being removed and cannot figure out how I can work
around this.

allen replied on 10-Mar-09 10:39 PM
Hi Lewis,

My name is Allen Chen. It's my pleasure to work with you on this issue.

From your description, when CompareValidator is used the JS function hooked
to onchange event doesn't fire, right?

I did a simple test but it worked fine. Here's my test code:


function Test() {
var d = new Date();
document.getElementById("input1").value = d.getTime();
This input is for test. Don't input value in it.
ControlToCompare="TextBox2" ValueToCompare="Text"
ID="CompareValidator1" runat="server"

protected void Page_Init(object sender, EventArgs e)
this.TextBox1.Attributes.Add("onchange", "Test()");
this.TextBox2.Attributes.Add("onchange", "Test()");


When you input value into TextBox1 and TextBox2 you can see the Test()
function is always called when the value is changed.

Could you compare your code with mine to see if there's any difference? You
can also send me a demo that can reproduce this problem. My email is
allenc@microsoft.com. Please update here after sending the project in case
I missed that email.

Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:

Get notification to my posts through email? Please refer to

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
This posting is provided "AS IS" with no warranties, and confers no rights.
allen replied on 13-Mar-09 04:39 AM
Hi Lewis,

Do you have any progress on this issue? Have you sent me the project?

Allen Chen
Microsoft Online Support
allen replied on 13-Mar-09 04:39 AM
Hi Lewis,

Do you have any progress on this issue? Have you sent me the project?

Allen Chen
Microsoft Online Support
Lewis Holmes replied on 13-Mar-09 07:09 AM
Hi Allen

Thanks for your reply. I agree that I ned to reproduce this in a sample
application but that will take some time due to complexity of the code
involved. I will try and strip the code down to the bare minimum needed to
reproduce the issue.

I am aiming to spend sometime to do this next week so I should be able to
send you a sample then. I wish it could be quicker but I have other work
that needs to be completed urgently.


allen replied on 18-Mar-09 03:14 AM
Hi Lewis,

Just check back if you have created the demo. Please let me know if you have
done it.

Allen Chen
Microsoft Online Support