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
 

 

24 Comments

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

  • 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


  • 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

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

  • @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

  • 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

  • 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

  • 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

  • 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?

  • 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

  • Annoying problem. Thanks for the workaround.

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

    Greetings Eric Gehring

  • Very annoying problem. Thanks for the workaround.

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

  • private void SetChromeFix()

    &nbsp; &nbsp; &nbsp; &nbsp;{

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

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string browser = Context.Request.UserAgent;

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!String.IsNullOrEmpty(browser) &amp;&amp; browser.IndexOf(&quot;Chrome&quot;) &gt; -1 &amp;&amp; !Page.ClientScript.IsStartupScriptRegistered(&quot;GoogleChromeValidatorHookupEventFix&quot;))

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Page.ClientScript.RegisterStartupScript(typeof(Page), &quot;GoogleChromeValidatorHookupEventFix&quot;, @&quot;function redefineValidatorHookupEvent() {

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (typeof (ValidatorHookupEvent) == &quot;&quot;function&quot;&quot;) {

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ValidatorHookupEvent = function(control, eventType, functionPrefix) {

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var ev;

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eval(&quot;&quot;ev = control.&quot;&quot; + eventType + &quot;&quot;;&quot;&quot;);

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (typeof(ev) == &quot;&quot;function&quot;&quot;) {

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ev = ev.toString();

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ev = ev.substring(ev.indexOf(&quot;&quot;{&quot;&quot;) + 1, ev.lastIndexOf(&quot;&quot;}&quot;&quot;));

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else {

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ev = &quot;&quot;&quot;&quot;;

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var func = new Function(&quot;&quot;event&quot;&quot;, &quot;&quot; var evt = event; &quot;&quot; + functionPrefix + &quot;&quot; &quot;&quot; + ev);

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eval(&quot;&quot;control.&quot;&quot; + eventType + &quot;&quot; = func;&quot;&quot;);

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;redefineValidatorHookupEvent();

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;, true);

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

    &nbsp; &nbsp; &nbsp; &nbsp;}

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

  • 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

  • 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 !

  • 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!! :-)

  • Ditto mucho mucho appreciated!!

  • 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?

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

  • Thanks a lot for your help.
    it save my lots of time and to change validations to server side.

    Thanks again

  • You can check out the solution for this problem.

    http://dotnetguts.blogspot.com/2009/05/dropdownlist-autopostback-problem-with.html

Comments have been disabled for this content.