在自动回发功能后将焦点设置回正确的文本框(Page.SetFocus 不能解决问题) [英] Set focus back to the proper textbox after autopostback function (Page.SetFocus doesn't solve the issue)

查看:25
本文介绍了在自动回发功能后将焦点设置回正确的文本框(Page.SetFocus 不能解决问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,这可能看起来重复,但事实并非如此.互联网上的每个解决方案都向您展示了如何将注意力集中在触发事件的文本框上.

Now, this MAY look a duplicate, but it's not. Every solution on the internet shows you how to get focus on the textbox that fired the event.

但是如果用户按下 Tab 键呢?应该具有焦点的文本框是下一个.因此,我们采取了一种解决方法,并将重点放在 TabIndex 高于触发事件的文本框上.

But what if the user presses tab? The textbox that should have focus is the next one. So we do a workaround and focus on the textbox that have TabIndex higher than the one that fired the event.

但是如果用户按下 Shift+tab 会怎样?更糟糕的是:如果用户点击另一个随机文本框会怎样?

But then what if the user presses Shift+tab? Even worse: what if the user clicks on another random textbox?

这就是问题所在.我认为这里不需要代码,因为这是将焦点设置在具有自动回发功能的文本框上的通用解决方案.如果需要代码,请在评论中询问.

This is the issue. I don't think a code is required here, because it's a general solution to set focus on textboxes that have autopostback function. If code is required, please ask in the comments.

推荐答案

以下内容将允许您做您想做的事:

The following will allow you to do what you want:

我们需要做的是让 js 协助下一步将是什么控件,在这种情况下,任何获得焦点的控件(无论是通过 Tab、Shift-Tab、单击还是从文本中跳出的任何控件组合框并放到不同的控件上).通过使用 WebMethod,我们可以将此信息传递到服务器以获取 AutoPostBack 焦点.

What we need to do is have js assist with what control will be next, in this case any control that is getting focus (whether it be via tab, shift-tab, click, or whatever control combination leaps out of the text box and onto a different control). By utilizing a WebMethod we can pass this information onto the server for AutoPostBack focus.

[WebMethod]
public static void set_nextFocus(string id)
{
    _toFocus = id;
}

很简单,_toFocus 是类变量静态字符串_toFocus,它保存了下一个要聚焦的控件的值.

Simple enough, _toFocus is class variable static string _toFocus, it holds the value of the next control to focus.

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        //sets focus to the proper control
        Page.SetFocus(Page.FindControl(_toFocus));
    }
}

JavaScript

<script type="text/javascript">
    function setFocus(x) {
        PageMethods.set_nextFocus(x);
    }
</script>

ASP 控件

在这个例子中,一个文本框.注意 OnFocusIn 的使用.它是ASP控件的expando属性,实现无服务端定义,还原为javascript的onfocusin属性.

ASP controls

In this example, a TextBox. Note the use of OnFocusIn. It is an expando attribute of the ASP control which will realize there is no server-side definition, and revert to javascript's onfocusin attribute.

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" TabIndex="1" 
        ontextchanged="TextBox1_TextChanged" OnFocusIn="setFocus(this.id)" >
</asp:TextBox>

此外,为了使用 PageMethods,您必须在表单中启用它,如下所示:

Also, in order to use PageMethods you must enable it within the form, like so:

<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

这篇关于在自动回发功能后将焦点设置回正确的文本框(Page.SetFocus 不能解决问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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