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

L

Lewis Holmes

Hi

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
JavaScript.

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
JavaScript.

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.
 
A

Allen Chen [MSFT]

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:

Aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function Test() {
var d = new Date();
document.getElementById("input1").value = d.getTime();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
This input is for test. Don't input value in it.
<input id="input1" />
<br />
TextBox1:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
TextBox2:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CompareValidator ControlToValidate="TextBox1"
ControlToCompare="TextBox2" ValueToCompare="Text"
ID="CompareValidator1" runat="server"
ErrorMessage="CompareValidator"></asp:CompareValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>

Aspx.cs:
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
(e-mail address removed). Please update here after sending the project in case
I missed that email.

Regards,
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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

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
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Allen Chen [MSFT]

Hi Lewis,

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

Regards,
Allen Chen
Microsoft Online Support
 
A

Allen Chen [MSFT]

Hi Lewis,

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

Regards,
Allen Chen
Microsoft Online Support
 
L

Lewis Holmes

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.

Regards

Lewis
 
A

Allen Chen [MSFT]

Hi Lewis,

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

Regards,
Allen Chen
Microsoft Online Support
 

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

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top