Setting focus to last focused element before postback

I found some solution to a problem of mine where the Ajax postback would lose the focus on the element after it’s processed. What I found is a Javascript solution that sets the focus back to the last focused element so it won’t disappear. Anyway, that makes another problem which didn’t exist before I added these Javascript functions – If I modify some TextBox because the postback is processed, it will set the values back to what it was when the Ajax function was called. Is there any solution to that? This is my functions:

var lastFocusedControlId = "";

function focusHandler(e) {
    document.activeElement = e.originalTarget;
}

function appInit() {
    if (typeof (window.addEventListener) !== "undefined") {
        window.addEventListener("focus", focusHandler, true);
    }
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);
}

function pageLoadingHandler(sender, args) {
    lastFocusedControlId = typeof (document.activeElement) === "undefined"
        ? "" : document.activeElement.id;
}

function focusControl(targetControl) {
    if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
        var focusTarget = targetControl;
        if (focusTarget && (typeof (focusTarget.contentEditable) !== "undefined")) {
            oldContentEditableSetting = focusTarget.contentEditable;
            focusTarget.contentEditable = false;
        }
        else {
            focusTarget = null;
        }
        targetControl.focus();
        if (focusTarget) {
            focusTarget.contentEditable = oldContentEditableSetting;
        }
    }
    else {
        targetControl.focus();
    }
}
function pageLoadedHandler(sender, args) {
    if (typeof (lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") {
        var newFocused = $get(lastFocusedControlId);
        if (newFocused) {
            focusControl(newFocused);
        }
    }
}
Sys.Application.add_init(appInit);
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <center id="user_management">
        <div id="head" runat="server"><center><h2 style="margin:5px; text-decoration:underline">הרשמה</h2></center></div>
        <hr style="margin:0"/>
        <div id="container_" runat="server" style="background-color:White; padding:5px; margin-bottom:7px; margin-bottom:0px;">
            <div id="eborder" style="background-color:Silver; display:inline-block; padding:5px; margin-bottom:7px; margin-bottom:0px;">
                <div dir="rtl" style="text-align:center; background-color:White; padding-bottom:5px; padding-top:5px; display:inline-block; border:2px inset;" runat="server" id="user_info">
                    <center id="manage_user" runat="server" style="display: inline-block;">
                            <div id="Message" runat="server" dir="rtl" style="font-weight:bold; border:2px solid black; display:none; margin:auto 7px auto 7px; padding-left:7px; padding-right:7px;">
                                <%-- AJAX messages showing up here if managing specific user --%>
                            </div>
                        <div id="user_info_all" runat="server">
                            <table dir="rtl" style="text-align:left" id="table_user" runat="server">
                                <tr><td>שם משתמש:</td><td><asp:TextBox TextMode="SingleLine" ID="reg_user" runat="server" MaxLength="50" OnTextChanged="reg_user_TextChanged" AutoPostBack="true"></asp:TextBox></td></tr>
                                <tr><td>סיסמא:</td><td><input type="text" id="reg_pass" runat="server" maxlength="50"  /></td></tr>
                                <tr><td>גיל:</td><td><input type="text" id="reg_age" runat="server" maxlength="2" /></td></tr>
                                <tr><td colspan='2' style="text-align:center"><asp:Button runat="server" ID="btnModify" OnClientClick="CloseUserInfo()" Text="הירשם" UseSubmitBehavior="false" /></td></tr>
                            </table>
                        </div>
                    </center>
                </div>
            </div>
        </div>
        <hr style="margin-top:0"/>
    </center>
</ContentTemplate>


Download setting.focus.to.last.focused.element.before.postback.zip
Direct Link


Download


Download setting.focus.to.last.focused.element.before.postback.zip
Mediafire


Download


Download setting.focus.to.last.focused.element.before.postback.zip
HitFile


Download