设置始终聚焦于文本框(ASP.NET、Updatepanel)

发布于 2024-09-26 03:47:46 字数 234 浏览 0 评论 0原文

嘿, 在我开始写问题之前,我会原谅我糟糕的英语,我希望你能理解我。

我在 ASP.NET Web 应用程序中有一个 AJAX 更新面板。在这个更新面板中有一个 动态搜索结果的文本框。当我开始在文本框中写入时,结果就像谷歌建议的那样。

现在,焦点必须始终位于文本框(输入字段)上,现在测量用户单击的位置。

目前,当用户开始键入时,ASP.NET 更新面板会在几秒钟后刷新。

感谢您的帮助:-)

Hey,
Before I start to write my problem, I will excuse for my bad English and I hope you can understand me.

I have in a ASP.NET Webapplication an AJAX Updatepanel. In this Updatepanel is a
Textbox for dynamic search results. When I start to write in the Textbox, the results comes like Google suggest.

Now, the focus must be always on the Textbox (inputn field), now metter whereto the User clicks.

Currently the ASP.NET updatepanel refreshed after a few seconds when the User starts to type.

Thanks for help :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永言不败 2024-10-03 03:47:46

当 updatepanel 完成更新 html dom 时会发生一个事件

Sys.WebForms.PageRequestManager.getInstance().add_endRequest

尝试这个

function EndRequestHandler() {  
//get focus on the textbox
myTextbox.focus(); }

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

there is an event when updatepanel finish updated html dom

Sys.WebForms.PageRequestManager.getInstance().add_endRequest

try this

function EndRequestHandler() {  
//get focus on the textbox
myTextbox.focus(); }

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
雨后咖啡店 2024-10-03 03:47:46

这很有趣,但这是一个可能的解决方案。这个想法是:如果用户离开文本框(onblur),则将他带回文本框(focusOnTxt 函数):

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function focusOnTxt(sender) {
            sender.focus(); 
            sender.value = sender.value;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="upnl" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="txt" runat="server"
          onblur="focusOnTxt(this)"></asp:TextBox>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

在 Page_Load 上:

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txt.Focus();
    }
}

That is pretty fun but here is a possible solution. The idea is: if user gets out of the textbox (onblur), then take him back to the textbox (focusOnTxt function):

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function focusOnTxt(sender) {
            sender.focus(); 
            sender.value = sender.value;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="upnl" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="txt" runat="server"
          onblur="focusOnTxt(this)"></asp:TextBox>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

And on Page_Load:

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txt.Focus();
    }
}
趁年轻赶紧闹 2024-10-03 03:47:46

Page.Load 中的简单 SetFocus fe 应该可以工作:

ScriptManager1.SetFocus (TextBox1.ClientID)

更新:根据 这篇文章以下作品...

将此脚本添加到页面头部的脚本块中:

function SetEnd(TB){
    if (TB.createTextRange){
        var FieldRange = TB.createTextRange();
        FieldRange.moveStart('character', TB.value.length);
        FieldRange.collapse();
        FieldRange.select();
    }
}

然后添加将 onfocus 事件添加到文本框:

onfocus="SetEnd(this)"

在代码隐藏的 Page.Load 或 TextChanged-Event 处理程序中添加标准 SetFocus 调用:

ScriptManager sm = ScriptManager.GetCurrent(this);
sm.SetFocus(myTextBox)

A simple SetFocus f.e. in Page.Load should work:

ScriptManager1.SetFocus (TextBox1.ClientID)

UPDATE: according to this post following works...

Add this script into a script block in your page's head:

function SetEnd(TB){
    if (TB.createTextRange){
        var FieldRange = TB.createTextRange();
        FieldRange.moveStart('character', TB.value.length);
        FieldRange.collapse();
        FieldRange.select();
    }
}

Then add the onfocus event to your Textbox:

onfocus="SetEnd(this)"

In your codebehind's Page.Load or TextChanged-Event handler add the standard SetFocus call:

ScriptManager sm = ScriptManager.GetCurrent(this);
sm.SetFocus(myTextBox)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文