Bug with Latest Google Chrome and ASP.NET Validation

Hello,

This is something that drove me insane today, I have a dropdown control with a required field validator on this control. the dropdown is also set to autopostback. Now up until after lunch today this was working fine in chrome but after lunch it stopped. Turns out my Chrome updated to the latest version which seemed to break validation in the case which is shown below try the below on a page in the latest Chrome you will see that a postback will not happen, remove the validator and postback will work just fine.

Selected: <%= DropDown.SelectedValue %>
<asp:DropDownList ID="DropDown" runat="server" AutoPostBack="true">
<asp:ListItem Value="" Text=""></asp:ListItem>
<asp:ListItem Value="0" Text="0"></asp:ListItem>
<asp:ListItem Value="1" Text="1"></asp:ListItem>
<asp:ListItem Value="2" Text="2"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredTest" runat="server" ControlToValidate="DropDown" ErrorMessage="Must Select Item">
</asp:RequiredFieldValidator>  

I thought this must be an issue in the validation library for ASP.NET and after time narrowed it down to the ValidatorHookupEvent method in the library:

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;");
}
 
Now the line in bold is in question: func = new Function("event", functionPrefix + " " + ev);, this is wrapping the ev argument which is the original
onchange event on the dropdown in our case the postback and the ValidatorOnChange method, if you look at how chrome wraps the function in the
onchange when you view the value of func after it has wrapped the two together:
function anonymous(event) { ValidatorOnChange(event); with (this.ownerDocument ? this.ownerDocument : {}) { with (this.form ? this.form : {}) { with (this) { return (function(evt){javascript:setTimeout('__doPostBack(\'DropDown\',\'\')', 0)}).call(this, evt); } } } } 

And in firefox the wrapping is not like chrome as you can see chrome has an evt argument where firefox is different:
function anonymous(event) { ValidatorOnChange(event);javascript: setTimeout("__doPostBack('DropDown','')", 0); } 

Now the thing to note in Chrome look at the onchange of the dropdown, say alert the items onchange event to view what it looks like, it has an evt argument which seems to cause the issue.

function onchange(evt) {
    with (this.ownerDocument ? this.ownerDocument : {}) { with (this.form ? this.form : {}) { with (this) { return (function(evt){javascript:setTimeout('__doPostBack(\'DropDown\',\'\')', 0)}).call(this, evt); } } } 
}   

In firefox the onchange in firefox it uses an event argument and not evt like chrome.
function onchange(event) {
javascript: setTimeout("__doPostBack('DropDown','')", 0);

I found a simmilar problem and got an answer: http://code.google.com/p/chromium/issues/detail?can=2&q=&colspec=ID%20Stars%20Pri%20Area%20Type%20Status%20Summary%20Modified%20Owner&sort=&id=650, Look at the bottom, particularly this comment:
The additional function wrapper was added in r2111 
(http://src.chromium.org/viewvc/chrome?view=rev&revision=2111) and has been in Chrome
from version 0.2.153.0.

From what I can see this is what is causing the issue, now who is at blame I cannot say but this is mighty anyoing :(, I knew this was working and for it to just
stop was painful and took some time to debug. But now that I know the issue it is time to tackle fixing it. There is a bit more that I have missed but it is late and I just got
down what I had for the moment. I will look into this more and keep you informed of what I come across.



Stay Tuned
Stefan
 

 

Published Tuesday, November 4, 2008 9:48 PM by stefan.sedich

Comments

# re: Bug with Latest Google Chrome and ASP.NET Validation

Tuesday, November 4, 2008 1:42 PM by RichardD

So it works in everything except the latest release of Chrome? I think "who is at blame" is quite obvious!

# re: Bug with Latest Google Chrome and ASP.NET Validation

Tuesday, November 4, 2008 7:14 PM by stefan.sedich

Good point there :P, I tested this same scenario with JQuery and there are no issues, but JQuery is much smarter in the way it joins the handlers for the event.

I will look at fixing the issues in the library so that it will work.

Thanks

Stefan

# re: Bug with Latest Google Chrome and ASP.NET Validation

Saturday, January 3, 2009 6:34 AM by Khushal Patel

Hello Stefan,

I am facing the same problem in Chrome, I have used your script but it does not doing anything, actually the script function ValidatorHookupEvent not called when i change the select an item from the dropdown list, are you sure this is working on your PC in the chrome, please take a look at this issue my chrome version is 1.0.154.36

Thanks

Khushal

# re: Bug with Latest Google Chrome and ASP.NET Validation

Thursday, January 8, 2009 3:34 AM by Amit Saini

Thanks a lot for the Solution. It really saved our life.

# re: Bug with Latest Google Chrome and ASP.NET Validation

Thursday, January 8, 2009 3:51 AM by stefan.sedich

@Amit

No worries glad it helped :), I have not pursued the issue further but might do some day soon.

@Khushal

where did you put the event? can you email me some source code? stefan [dot] sedich [at] gmail [dot] com

Cheers

Stefan

# re: Bug with Latest Google Chrome and ASP.NET Validation

Friday, January 9, 2009 6:59 AM by Khushal

Hi Stefan,

Thanks for replying, mistake is on my side i am using a page which has a MasterPage and i put script in the page but form tag is on the masterpage, so i put it into the masterpage and now it works fine ... [:)] thanks so much for your helpp...

Thanks

Khushal

# re: Bug with Latest Google Chrome and ASP.NET Validation

Friday, January 9, 2009 7:16 AM by stefan.sedich

Khushal,

No worries, I am sorry it took me soo long this one slipped through my SPAM filter :\, good to see you have it all up and running.

Thanks

Stefan

# re: Bug with Latest Google Chrome and ASP.NET Validation

Sunday, January 11, 2009 9:26 PM by stefan.sedich

Robin,

Good to see this has helped someone :). I am actually going to make a note to look into a better solution for doing this, I have a solution that uses JQuery for the event hookup but the quick hack above was more than enough to get this working.

I will keep you posted on what I can come up with, I have not seen any recent bugs logged with Chrome/ASP.NET, so I assume that this is not a widely known issue. If anyone knows of any more people with this issue logged anywhere with more info forward me the link.

Cheers

Stefan

# re: Bug with Latest Google Chrome and ASP.NET Validation

Monday, January 12, 2009 5:10 AM by Andrea Maschio

Hi, your fix works very well. Is this a chrome or Asp Net Bug? I'd like to notify the issue to the proper team, did you already?

# re: Bug with Latest Google Chrome and ASP.NET Validation

Wednesday, January 14, 2009 4:10 AM by stefan.sedich

Jacob,

Amazing I helped someone else :), I am yet to find out the broader solution to this issue or if it is commonly known. I assume by most people finding this that it might not be as well known as I thought it might be.

I assure you I want to look into this further but the next 2 weeks for me are filled up with my big move, assured once I am settled in I am going to get right onto this but it might be 2-3 weeks away. If you can flick me an email at stefan [dot] sedich [at] gmail [dot] com. And remind me I will work with you to see if we can log this somewhere and get a more accepted formal answer/solution.

As I say deffinately on my todo list but has just fallen through the cracks with my looming deadlines :(.

Cheers

Stefan

# re: Bug with Latest Google Chrome and ASP.NET Validation

Wednesday, January 14, 2009 9:38 AM by Alf Lervåg

Annoying problem.  Thanks for the workaround.

# re: Bug with Latest Google Chrome and ASP.NET Validation

Tuesday, January 27, 2009 5:58 PM by EricNL

It works for me too. Stefan, Thank u for your solution. Jacob also for the help.

Greetings Eric Gehring

# re: Bug with Latest Google Chrome and ASP.NET Validation

Wednesday, January 28, 2009 6:08 PM by Cmac

Very annoying problem.  Thanks for the workaround.

# re: Bug with Latest Google Chrome and ASP.NET Validation

Tuesday, February 3, 2009 3:31 PM by Scott

Add me to the thankful - this was driving me insane.

# re: Bug with Latest Google Chrome and ASP.NET Validation

Wednesday, February 4, 2009 5:08 PM by Pat Patterson

private void SetChromeFix()

       {

           //// Adds a script that fixes the .Net ValidatorHookupEvent for Google Chrome

           string browser = Context.Request.UserAgent;

           if (!String.IsNullOrEmpty(browser) && browser.IndexOf("Chrome") > -1 && !Page.ClientScript.IsStartupScriptRegistered("GoogleChromeValidatorHookupEventFix"))

           {

               Page.ClientScript.RegisterStartupScript(typeof(Page), "GoogleChromeValidatorHookupEventFix", @"function redefineValidatorHookupEvent() {

               if (typeof (ValidatorHookupEvent) == ""function"") {

                   ValidatorHookupEvent = function(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 = new Function(""event"", "" var evt = event; "" + functionPrefix + "" "" + ev);

                   eval(""control."" + eventType + "" = func;"");

                   }

               }

           }

           redefineValidatorHookupEvent();

           ", true);

           }

       }

# re: Bug with Latest Google Chrome and ASP.NET Validation

Friday, February 13, 2009 5:11 AM by Alex Roberts

This fix should go into the Golden Hall of Fixes, and Google should hang their heads in shame. To be honest, I don't exactly know how it works (an explanation wouldn't go amiss), but work it does, so more power to your elbow I say. You're a clever chap Stefan.

# re: Bug with Latest Google Chrome and ASP.NET Validation

Wednesday, February 25, 2009 6:38 AM by Mats Bernerfalk

Works Fine!

Thanks for help!

I made a port to VB.NET:

Protected Sub SetChromeFix()

       Dim browser As String = Context.Request.UserAgent

       If Not String.IsNullOrEmpty(browser) And browser.IndexOf("chrome") > -1 And Not Page.ClientScript.IsStartupScriptRegistered("GoogleChromeValidatorHookupEventFix") Then

           Dim script As String

           script = "function redefineValidatorHookupEvent() {" & _

           "if (typeof (ValidatorHookupEvent) == ""function"") {" & _

           "ValidatorHookupEvent = function(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 = new Function(""event"", "" var evt = event; "" + functionPrefix + "" "" + ev);" & _

           "eval(""control."" + eventType + "" = func;"");" & _

           "}}}" & _

           "redefineValidatorHookupEvent();"

           ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "GoogleChromeValidatorHookupEventFix", script, True)

       End If

   End Sub

# re: Bug with Latest Google Chrome and ASP.NET Validation

Saturday, March 14, 2009 5:14 PM by R. Jones

Heya Stefan,

Just wanted to offer sincere kudos to you for your diligence in looking into this issue and writing it up for everybody's sake.  I just got broadsided by this issue and want to offer my thanks for saving me the hours (and possibly days) of unrolling this.  Thanks kindly !

# re: Bug with Latest Google Chrome and ASP.NET Validation

Saturday, March 21, 2009 12:09 PM by Philippe Monnet

I had to include the Chrome-specific Javascript at the bottom of my pages to ensure the timing of the event handler redefine process works right. Not Chrome can access the web app correctly! Great job Stefan!! :-)

# re: Bug with Latest Google Chrome and ASP.NET Validation

Tuesday, March 31, 2009 3:40 AM by John DIlon

Ditto mucho mucho appreciated!!

# re: Bug with Latest Google Chrome and ASP.NET Validation

Monday, April 13, 2009 3:10 PM by Parixit

None of the above worked.

i don't know how you people have it worked.

it doesn't work in latest chrome.

i tried all things out, but didn't work.

can u give the working code.

is there anything else to add other than just a javascrip?

# re: Bug with Latest Google Chrome and ASP.NET Validation

Wednesday, April 15, 2009 8:21 PM by Evan

Thank you very much!  Great, easy, quick fix.  Thanks also to Pat for the nifty function.

# re: Bug with Latest Google Chrome and ASP.NET Validation

Monday, May 11, 2009 5:10 AM by Mona Patel

Thanks a lot for your help.

it save my lots of time and to change validations to server side.

Thanks again

# re: Bug with Latest Google Chrome and ASP.NET Validation

Monday, May 11, 2009 7:03 AM by SMC

Thanks a lot, It solves my problem

# re: Bug with Latest Google Chrome and ASP.NET Validation

Monday, May 18, 2009 11:56 PM by dotnetguts

You can check out the solution for this problem.

dotnetguts.blogspot.com/.../dropdownlist-autopostback-problem-with.html