最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set always Focus on Textbox (ASP.NET, Updatepanel) - Stack Overflow

programmeradmin0浏览0评论

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 es 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 :-)

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 es 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 :-)

Share edited Oct 4, 2010 at 12:18 anishMarokey 11.4k2 gold badges35 silver badges47 bronze badges asked Oct 4, 2010 at 12:06 PatrikPatrik 1,1295 gold badges18 silver badges37 bronze badges 2
  • I'm not quite sure about this answer, but what happens when you put the focus on the control in the Page_load event? – Emerion Commented Oct 4, 2010 at 12:11
  • The focus must be always in the textbox, whereever the User is clicking.. – Patrik Commented Oct 4, 2010 at 14:03
Add a ment  | 

3 Answers 3

Reset to default 3

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);

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();
    }
}

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)
发布评论

评论列表(0)

  1. 暂无评论