微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在C#中检测聚焦控制

我有一个更新面板,它在页面的一部分上引起回发,并且在回发之后,具有焦点的控件(不在更新面板中)失去焦点.如何识别哪个控件具有焦点并保存该值,以便在页面重新加载时可以重新聚焦它.谢谢.

解决方法

首先,我将焦点绑定在所有输入上并保留最后一个焦点控件ID.然后在UpdatePanel完成加载后,我将焦点设置为最后一个

// keep here the last focused id
var LastFocusedID = null;

function watchTheFocus()
{
  // on every input that get the focus,I grab the id and save it to global var
  $(":input").focus(function () {
     LastFocusedID = this.id;
  });   
}

var prm = Sys.WebForms.PageRequestManager.getInstance();    
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);

function InitializeRequest(sender,args) {      
}

// after the updatePanel ends I re-bind the focus,and set the focus
//  to the last one control
function EndRequest(sender,args) {
    if(LastFocusedID != null)
        $('#' + LastFocusedID).focus();        
    watchTheFocus();
}

jQuery(document).ready(function() 
{       
    watchTheFocus();
});

唯一的想法是我使用jQuery来制作它,但我在这里提出我的想法,你可以用更多的代码用jQuery来实现它.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐