Javascript 如何防止Focus()方法将页面滚动到顶部

Javascript 如何防止Focus()方法将页面滚动到顶部,javascript,jquery,asp.net,css,ajax,Javascript,Jquery,Asp.net,Css,Ajax,我在gridview中设置了一组文本框,在丢失到预期文本框后,我使用Focus()方法恢复焦点。问题是: 页面(可滚动),当我调用Focus方法时,在text changed事件中,页面跳转到顶部。这是一种令人困惑的行为 我的问题是: 是否有某种方法可以防止Focus()方法将页面跳转到顶部 我的代码: protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e) {

我在gridview中设置了一组文本框,在丢失到预期文本框后,我使用
Focus()
方法恢复焦点。问题是:

页面(可滚动),当我调用Focus方法时,在text changed事件中,页面跳转到顶部。这是一种令人困惑的行为

我的问题是:

是否有某种方法可以防止
Focus()
方法将页面跳转到顶部

我的代码:

protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e)
        {

            calc();
            int index = ((System.Web.UI.WebControls.GridViewRow)(((RadTextBox)sender).Parent.NamingContainer)).DataItemIndex;

            ((RadTextBox)gv_Evaluation.Rows[index + 1].Cells[3].FindControl("txt_evaluateWeights")).Focus();//Here is the problem. 
        }
注意:

  • 我使用了
    asp:TextBox
    ,也遇到了同样的问题

  • 更新面板中的我的栅格视图


编辑:

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;
        }
        try {
            targetControl.focus();
            if (focusTarget) {
                focusTarget.contentEditable = oldContentEditableSetting;
            }
        }
        catch (err) { }
    }
    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);
Javascript解决方案:

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;
        }
        try {
            targetControl.focus();
            if (focusTarget) {
                focusTarget.contentEditable = oldContentEditableSetting;
            }
        }
        catch (err) { }
    }
    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);

您可以尝试使用maintainScrollPositionOnPostBack选项:

<%@ Page MaintainScrollPositionOnPostback="true" %> 

您可以使用滚动到文本框:

$(...).scrollTo( $('<%= txt_evaluateWeights.ClientID %>') )
$(…)。滚动到($('')

您可以在设置焦点后尝试此操作

Response.Write("<script type='text/javascript'>$(...).scollTo( $('<%= txt_evaluateWeights.ClientID %>') )
</script>");
Response.Write($(…).scollTo($('')
");

这个问题已经被问了很久了

这是ASP.NET的JS库中的一个bug,解决方法很粗糙:

var oldScroll = window.scrollTo;
window.scrollTo = function(){};

//... the ugly ASP.NET stuff

window.scrollTo = oldScroll;

解决这一问题的最终解决方案是停止使用Microsoft ASP.NET,开始使用一种像样的技术,允许您编写客户端脚本。

与其尝试修复Focus方法的行为方式,还有一种替代方法。我还没有尝试过,但是,使用了一些客户端javascript来记住哪个控件具有焦点,然后在UpdatePanel刷新后将焦点放回该对象上。请务必阅读注释,了解脚本的一些更新


如果使用此方法,您将删除代码隐藏中要关注的调用,并让此脚本在客户机上处理它。

如果您使用的是更新面板和jquery,那么解决方案非常简单。假设您的文本框ID为tbDeliveryCost。不要使用tbDeliveryCost.Focus(),请执行以下操作:

string script = string.Format("$('#{0}').focus();", tbDeliveryCost.ClientID);
ScriptManager.RegisterStartupScript(this, this.GetType(), "SetFocus", script, true);

嗯,你认为ASP.NET不是一个像样的技术吗?!!这绝对不值得花那么多钱但我知道的是,SOF是用这项技术构建的。我仍然不会推荐给任何人作为记录,堆栈溢出是用ASP.NET MVC构建的,这与使用WebForms和UpdatePanel完全不同。非常感谢我们提供了这项伟大的解决方案。我编辑了我的问题,您是否建议对此代码进行任何增强?目前我看不出它有任何问题(实际上没有使用它)。所以,如果它在不同的浏览器中都能正常工作,那么我就不用管它了。但是
==
在javascript中意味着什么呢?不是打字错误。这意味着它会检查这两个值是否相同,以及它们是否也是相同的类型。例如,1==“1”为真,但1==“1”为假。