[RESOLVED]Click Event handler being run twice

I have an Asp.Net MVC 4 web app being run in debug in VS 2012 Pro on IE8.

I have this click event handler on an MVC partial view.

$(document).on("click", "a[id*='btnSave']", (function (e) {
        debugger
        var subLineId = e.target.id.toString().substr(7)
        //$("tr[data-pr-sublines ='prSubLine-" + subLineId + "']");
        var sURLSectionB = '@Url.Action("UpdatePRSubLine", "AJAXFunctions")';
        var returnedValue = "";
        var bError = false;
        var options = {
            url: sURLSectionB,
            data: {
                __RequestVerificationToken: $('input[name=__RequestVerificationToken]').val(),
                PRSLID: subLineId,
                PRSLNumber: $("input[id*='PRSubLine_PRSubLineNumber']").val(),
                PRSLQty: $("input[id*='PRSubLine_PRSubLineQty']").val()
            },
            type: "POST",
            cache: false,
            async: false,
        };
         //alert(options.url);
        $.ajax(options).done(function (data) {
            debugger
            if (data != null) {
                if (data == "Good") {
                    returnedValue = data;
                }
                else {
                    bError = true;
                    alert("An Error has occurred.n" + data.toString());
                    returnedValue = "An Error has occurred.n" + data.toString();
                }
            }
            else {
                bError = true;
                alert("An Error has occurred.nIt was not possible to update the PR SubCLIN.");
                returnedValue = "An Error has occurred.nIt was not possible to update the PR SubCLIN.";
            }
        });
        debugger
        var test = bError;
        $("#PRSectionB").empty();
         //alert(returnedValue);
        var sURLSectionB = '@Url.Action("Edit", "PR")';
        alert(sURLSectionB);
        debugger
        var options = {
            url: sURLSectionB,
            type: "get",
            cache: false,
            async: false
        };
         //alert(options.url);
        $.ajax(options).done(function (data) {
            debugger
            $("#PRSectionB").replaceWith(data);
        });
        if (bError) {
            $("#LinesEdited").addClass("field-validation-error").text(returnedValue).show();
        }
        else {
            $("#LinesEdited").removeClass("field-validation-error").text("").hide();
        }
        return false;
     }));

This event handler is responding the the click event of a link tag on a partial view of that partial view.

I was puzzled the process started by the button click is run completely through one the each susequent time it is clicked, it results in the click even handler function being run twice.  The second time through, the two inputs are no longer generated and the
parameters storing there values are undefined.  This result bizarely in the the error flag being not being changed from false as the AJAX function refuses to run.  This then results in the "linesEdited" element being hidden.  I am unable to descipher the reason
why this is being run again.  I entered a similar thread about two weeks ago, but the only person that answered failed to note that I was using IE8 and advised me to use IE Developer ToolsNetwork (which does not exist in IE8).  Therefore as per past advice
of the forum moderators I am re-entering the thread this time with the diagnosed issue.

Hi  joeller,

As far as i know,F12 Developer Tools were introduced in Windows Internet Explorer 8  .I suggest that you can set a breakpoint on your javascript code ,and  press F12 to debug it. see if it has any error message.

Best  Regards,

Kevin Shen.

Kevin Shen – MSFT

As far as i know,F12 Developer Tools were introduced in Windows Internet Explorer 8  .

Developer Tools were developed in IE8.  However the Network tab which I was advised to use was not added until IE9.  I set a debugger in VS2012 and stepped through the code.  No errors were returned. The function was run twice for no apparent reason.

So I tried to do what you suggested using the F12 debug instead of the VS 2012 debug.

  1. So I start running the app in debug in VS2012. 
  2. I navigate to the page which calls the the function that is giving the problem.
  3. I click F12
  4. I go to the Script tab.
  5. I scroll through the page’s script until I get to the offending function.
  6. I set a break point at the debugger line in the function.
  7. I click "Start Debugging".
  8. A dialog comes up saying Internet Explorer has reach an error  and wants to close then offers the option to close or debug.  I click debug.
  9. Another dialog opens asking if I want to use a new instance of VS 2012 or a new instance of VS 2008 to debug.  I select VS 2012.  It also reports "An unhandled Win32 exception occurred in iexplorer.exe 8580"
  10. VS 2012 throws an exeption dialog while opening reporting "Unhandled exception at 0x725AC0D6 (comctl32.dll) in iexplore.exe: 0xC0000005: Access violation reading location 0x0C2913EC." twice.
  11. Then a window in the new instance of VS 2012 opens reporting "comctl32.pdb not loaded." and Exception location of "C:Windowswinsxsx86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.17514_none_41e6975e2bd6f2b2comctl32.dll"
  12. then it suggests Change existing PDB and binary search paths and retry: over a  checkbox listbox with an entry for Microsoft Symbol Servers unchecked.

I don’t know what any of this means but it does not seem to have anything to do with the JQery function running twice.

Hi joeller,

It is hard to reproduce your issue,If you don’t mind,please share your project which can reproduce your issue for us.

Best Regards,

Kevin Shen.

Kevin Shen – MSFT

It is hard to reproduce your issue,If you don’t mind,please share your project which can reproduce your issue for us.

I am not sure for what you are asking.  There is no way to up a zip file onto this site not to mention the accompanying database even if it wasn’t a violation of security., and far too big to upload on most sites which
do allow you to upload files.

Kevin? Anyone?

Hi joeller,

Since your post duplicate post ,here is the answer which from your new thread,i post it here for others if they have similar issue like you:

http://forums.asp.net/post/5802097.aspx

Best Regards,

Kevin Shen.

Everyone.  On the new thread that Kevin is talking about, (which I created due to lack of response here), I decided to do a work-around in which I examined a value which was present the first time through, but undefined on subsequent run throughs, and did
a return false from the function if the value was undefined before the function actually did anything.  For more details go to the link he specified above.

Leave a Reply