如何prevent对焦()方法从页面滚动到顶部 [英] How to prevent the Focus() method from scrolling the page to the top

查看:119
本文介绍了如何prevent对焦()方法从页面滚动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置在GridView文本框,我使用焦点()办法输给了预期的文本框后恢复的焦点。现在的问题是:

页面(滚动),当我调用Focus方法,在文本改变时,页面跳转到顶部。它是这样一个令人困惑的问题。

我的问题是:

有没有一些方法,以prevent从页面跳转到顶部的焦点()的方法?

我的code:

 保护无效txt_evaluateWeights_TextChanged(对象发件人,EventArgs的)
        {

            计算();
            INT指数= ((System.Web.UI.WebControls.GridViewRow)(((RadTextBox)sender).Parent.NamingContainer)).DataItemIndex;

            ((RadTextBox)gv_Evaluation.Rows [索引+ 1] .Cells [3] .FindControl(txt_evaluateWeights))焦点(); //这是问题所在。
        }
 

注意:

  • 我用的是 ASP:文本框,和同样的问题

  • 在一个更新面板我的网格视图


编辑:

的Javascript解决方法:

  VAR lastFocusedControlId =;

功能focusHandler(五){
    document.activeElement = e.originalTarget;
}

功能appInit(){
    如果(typeof运算(window.addEventListener)!==未定义){
        window.addEventListener(焦点访谈,focusHandler,真正的);
    }
    。Sys.WebForms.PageRequestManager.getInstance()add_pageLoading(pageLoadingHandler);
    。Sys.WebForms.PageRequestManager.getInstance()add_pageLoaded(pageLoadedHandler);
}

功能pageLoadingHandler(发件人,参数){
    lastFocusedControlId = typeof运算(document.activeElement)===不确定
        ? :document.activeElement.id;
}

功能focusControl(targetControl){
    如果(Sys.Browser.agent === Sys.Browser.InternetExplorer){
        VAR focusTarget = targetControl;
        如果(focusTarget&安培;及(typeof运算(focusTarget.contentEditable)==未定义)!){
            oldContentEditableSetting = focusTarget.contentEditable;
            focusTarget.contentEditable = FALSE;
        }
        其他 {
            focusTarget = NULL;
        }
        尝试 {
            targetControl.focus();
            如果(focusTarget){
                focusTarget.contentEditable = oldContentEditableSetting;
            }
        }
        赶上(错误){}
    }
    其他 {
        targetControl.focus();
    }

}

功能pageLoadedHandler(发件人,参数){
    如果(typeof运算(lastFocusedControlId)==不确定和放大器;!&安培;!lastFocusedControlId =){
        VAR newFocused = $ GET(lastFocusedControlId);
        如果(newFocused){
            focusControl(newFocused);
        }
    }
}

Sys.Application.add_init(appInit);
 

解决方案

而不是试图解决的焦点方法的行为方式,还存在另一种。我还没有尝试过,但这个页面有问题的讨论,以及一些客户端的JavaScript来记住哪个控制具有焦点,然后将焦点回到那个对象上后UpdatePanel的被刷新。请务必阅读注释某些更新脚本。

如果您使用此方法,您将删除通话将重点放在你的codebehind,并让与它这个脚本的交易在客户端上。

I have set of textboxes in a gridview and I use the Focus() method to restore the focus after losing to the intended text box. The problem is :

The page (scrollable) and when I call the Focus method, in the text changed event, the page jump to the top. It's such a confusing behavior.

My question is:

Is there some way to prevent the Focus() method from jumping the page to the top?

My code:

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

Note:

  • I use the asp:TextBox, and the same problem.

  • My grid view in an update panel


EDIT :

Javascript workaround:

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);

解决方案

Rather than trying to fix the way the Focus method behaves, there is an alternative. I haven't tried it, but this page has a discussion of the problem, and some client-side javascript to remember which control had the focus, and then put the focus back on that object after the UpdatePanel gets refreshed. Be sure to read the comment for some updates to the script.

If you use this method, you would remove the call to Focus on in your codebehind, and let this script deal with it on the client.

这篇关于如何prevent对焦()方法从页面滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆