TextBox2.Focus() don't work in Ajax

Last post 02-22-2008 10:32 AM by gt1329a. 3 replies.

Sort Posts:

  • TextBox2.Focus() don't work in Ajax

    02-22-2008, 8:20 AM
    • Participant
      1,555 point Participant
    • mycwcgr
    • Member since 08-07-2005, 8:47 AM
    • Posts 745

    TextBox2.Focus() don't work in Ajax, could you tell me how can focus a control in Ajax? thanks!


    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

        protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox2.Focus();
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
       
        </div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />
                </ContentTemplate>
            </asp:UpdatePanel>
        </form>
    </body>
    </html>

  • Re: TextBox2.Focus() don't work in Ajax

    02-22-2008, 8:51 AM
    • Member
      6 point Member
    • nofian.adi
    • Member since 02-17-2008, 2:30 PM
    • Posts 3

    you shoud use findcontrol() to get textbox2...

    and it usually in nested findcontrol...

  • Re: TextBox2.Focus() don't work in Ajax

    02-22-2008, 8:56 AM
    • Contributor
      2,539 point Contributor
    • mgodoy_desenv
    • Member since 02-22-2007, 7:12 PM
    • São Paulo - Brazil
    • Posts 453

    You can´t to do that cause the ASP.NET needs to render a client script. At assynchronous postback you can´t add client scripts.

    However, you can register client scripts at Page_Load when Page.IsPostBack property is false. You can to create a timer to read a property of any control (through setInterval client script function). Change that property at any assynchronous postback will activate the method in setInterval function to do anything what you to want. However, I think that is too work to low result, in your case.

    Excuse me for my way of writing. I don´t speak English so much.

  • Re: TextBox2.Focus() don't work in Ajax

    02-22-2008, 10:32 AM
    Answer
    • Star
      13,541 point Star
    • gt1329a
    • Member since 06-24-2002, 12:53 AM
    • Atlanta
    • Posts 2,236
    • ASPInsiders
      TrustedFriends-MVPs

    Use the Focus() method of the ScriptManager to do that during a partial postback.

    In this example: ScriptManager1.SetFocus(TextBox2) 

    Encosia - ASP.NET, jQuery, AJAX, and more.

    Latest article: Emulate ASP.NET validation groups with jQuery validation
Page 1 of 1 (4 items)