简体   繁体   English

C#ASP.NET在输入键触发保存按钮后将焦点设置在文本框上

[英]C# ASP.NET Set Focus On TextBox After Enter Key Triggers Save Button

I have a ASP.NET 4.5 Page which has a dynamically created list of text boxes 我有一个ASP.NET 4.5页面,该页面具有动态创建的文本框列表

I have set the default button for the asp.panel to be the Save button. 我已将asp.panel的默认按钮设置为“保存”按钮。

If the user is half way through filling out the text boxes and hits the enter key, the form saves, but then sets the focus back at the top of the page. 如果用户在填写文本框的途中途中途按下回车键,则会保存该表单,但将焦点重新设置在页面顶部。 How can I have it set the focus back on the last text box that was being edited? 如何将焦点重新设置到最后一个被编辑的文本框上?

You can add onfocus handler to all form elements via javascript/jquery which will save the id of the currently focused control to hidden field and read that id value when with javascript on document load so you can set focus on the right control. 您可以通过javascript / jquery将onfocus处理程序添加到所有表单元素,这会将当前关注的控件的id保存到隐藏字段中,并在使用JavaScript加载文档时读取该id值,以便您可以将焦点放在正确的控件上。

Example: 例:

<asp:HiddenField runat="server" ID="hfFocusedControl" />
<script>

$(document).ready(function(){
        var id = $('#hfFocusedControl').val()
        ;
        $('#'+id).focus();
        $('input').focus(saveIdOnFocus);


});
var saveIdOnFocus = function(e){
    var control = $(this)
        , id = control.attr('id')
    ;

    $('#hfFocusedControl').val(id);
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM