Ensure that the TextBox OnTextChanged (ASP.NET enters "setTimeout") fires before OnClick

Consider the following scenario:

<asp:TextBox ID="txt" runat="server" AutoPostBack="true" OnTextChanged="txt_TextChanged"></asp:TextBox>
<asp:Button ID="btn" runat="server" OnClick="btn_Click" CausesValidation="false" UseSubmitBehavior="false" />

when rendered, these two controls become:

<input name="txt" type="text" onchange="javascript:setTimeout('__doPostBack(\'txt\',\'\')', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="txt">
<input type="button" name="btn" onclick="javascript:__doPostBack('btn','')" id="btn">

thanks to setTimeout, the Click event is fired before the Change event.

After some research, I found that ASP.NET does this because of an old crash in some older versions of IE.

However, this causes problems because my click on the button hides the text box, causing some "Invalid callbacks or callbacks" errors.

How can I fix the execution order so that TextChanged always fires before clicking?

PS: Javascript/jQuery , ( , , eval )

+4
1

, , , - .
:

function removeSetTimeout() {
    var inputs = "select, input[type=text], input[type=number], input[type=date], input[type=time]";
    $(inputs).each(function (index, elem) {
        var change = $(elem).attr('onchange');
        if (change) {
            var patch = /setTimeout\('__doPostBack\(\\'.*?\\',\\'\\'\)', 0\)/.exec(change);
            if (patch) {
                change = change.replace(patch[0], patch[0].substring(12, patch[0].length - 5).replace(/\\/g, ''));
                $(elem).attr('onchange', change);
            }
        }
    });
}
0

Source: https://habr.com/ru/post/1680354/


All Articles