views:

155

answers:

4

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

+2  A: 

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);
jebberwocky
A: 

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();
    }
}
Musa Hafalır
Hi, thanks for help, the problem is that the whole text is selected.
Patrik
Are you using sender.value = sender.value;? It should prevent whole text selection. And I have tested that my solution has problem with chrome.
Musa Hafalır
Yes. Problem with chrome is no one for me. I must only support IE7 and IE8. Here A shortcut from my textbox. I hope it helps: <asp:TextBox ID="tbFilter" Text="" CssClass="bste_fld_sel_Inp" OnTextChanged="tbFilter_TextChanged" onkeydown="o_bste_fld_sel.search_keydown();" onkeyup="o_bste_fld_sel.search_keyup(this);" onblur="focusOnTxt(this)" AutoPostBack="True" autocomplete="off" runat="server"></asp:TextBox>
Patrik
A: 

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)
Tim Schmelter
Hey Tim, Problem ist now when I edit the text the cursor will jump to the end. The user must to be able edit the text. Sample: I entered stackowwwerflow and will correct it to stackoverflow. In you sample the cursor jumps always to the end of text.
Patrik
Tim, what changed this effect ? ScriptManager sm = ScriptManager.GetCurrent(this);sm.SetFocus(myTextBox)
Patrik
@Patrik: my change has nothing to do with your problems to maintain the cursor position on editing. Sorry, currently i have not the time to dig deeper into this issue.
Tim Schmelter
Okey Tim, still I give thanks to you :-)
Patrik
A: 

Hi, thanks for help, the problem is that the whole text is selected. But whatever the User on display is clicking, the cursor must be on the same position with focus ob the textbox.

Patrik