我在 gridview 中有一组文本框,我使用Focus()
失去目标文本框后恢复焦点的方法。问题是 :
页面(可滚动),当我调用 Focus 方法时,在文本更改事件中,页面跳转到顶部。这是一种非常令人困惑的行为。
我的问题是:
有什么办法可以防止Focus()
页面跳转到顶部的方法?
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:
我用asp:TextBox
,同样的问题。
我在更新面板中的网格视图
EDIT :
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);
除了尝试修复 Focus 方法的行为方式之外,还有另一种选择。我没试过,但是此页面有对该问题的讨论 http://couldbedone.blogspot.com/2007/08/restoring-lost-focus-in-update-panel.html,以及一些客户端 JavaScript 来记住哪个控件拥有焦点,然后在 UpdatePanel 刷新后将焦点放回到该对象上。请务必阅读评论以了解脚本的一些更新。
如果您使用此方法,您将在代码隐藏中删除对 Focus on 的调用,并让此脚本在客户端上处理它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)