Set Focus after Postback (asp.net/vb.net newbie)

Last post 04-21-2008 10:15 AM by MelissaL. 5 replies.

Sort Posts:

  • Set Focus after Postback (asp.net/vb.net newbie)

    04-17-2008, 4:54 PM
    • Loading...
    • MelissaL
    • Joined on 11-02-2007, 1:32 PM
    • Anoka, MN
    • Posts 6

    I have 3 text boxes in an AJAX Update Panel.  All user inputs have the Postback property set to True.  After I type something in TextBox1 and press the "Tab" key I am setting the focus to TextBox2, and so on.  The problem is if they type some data in TextBox1 and instead of pressing "Tab" they mouse click on TextBox3 the cursor goes to TextBox2 instead of having focus on TextBox3.  PLEASE HELP!

    DEFAULT.ASPX.VB CODE:

    Partial Class _Default
        Inherits System.Web.UI.Page
        Dim ControlName As String
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If IsPostBack = False Then
                TextBox1.Focus()
            End If
        End Sub
        Protected Sub SetNextQRControl(ByVal fld As Control)
            ScriptManager1.SetFocus(fld)
        End Sub
        Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            SetNextQRControl(TextBox2)
        End Sub

        Protected Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
            SetNextQRControl(TextBox3)
        End Sub

        Protected Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
            SetNextQRControl(TextBox1)
        End Sub
    End Class

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    DEFAULT.ASPX CODE:

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        Namespace="System.Web.UI" TagPrefix="asp" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Set Focus - AJAX Script Manager and Update Panel</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            &nbsp;
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
                        TabIndex="1"></asp:TextBox><br />
                    <br />
                    <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True"
                        TabIndex="2"></asp:TextBox><br />
                    <br />
                    <asp:TextBox ID="TextBox3" runat="server" AutoPostBack="True"
                        TabIndex="3"></asp:TextBox><br />
                </ContentTemplate>
            </asp:UpdatePanel>
        </form>
    </body>
    </html>
     

    ------------
    Melissa
  • Re: Set Focus after Postback (asp.net/vb.net newbie)

    04-17-2008, 11:29 PM

    You can use the ScriptManager's SetFocus method to do that. 

    Use this javascript code on the page:

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

    function endRequestHandler() {

    var textBox = $get("<%=MyTextBox.ClientID%>");

    if(textBox ) {

     textBox .focus();

    }

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Set Focus after Postback (asp.net/vb.net newbie)

    04-18-2008, 8:57 AM
    • Loading...
    • MelissaL
    • Joined on 11-02-2007, 1:32 PM
    • Anoka, MN
    • Posts 6
    Okay.  So how does that Javascript get called for each textbox?  Sorry, I don't get it.
    ------------
    Melissa
  • Re: Set Focus after Postback (asp.net/vb.net newbie)

    04-19-2008, 5:58 AM
    Answer

    Sets the browser focus to the specified control

    http://www.asp.net/AJAX/Documentation/Live/mref/O_T_System_Web_UI_ScriptManager_SetFocus.aspx

     

    You can use the following code to set the focus:
     

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

    To set focus and select text of a you have to use the following:
     

    ScriptManager.RegisterStartupScript(this, this.GetType(), "selectAndFocus", "$get('" + myTextBox.ClientID + "').focus();$get('" + myTextBox.ClientID + "').select();", true);
    http://forums.asp.net/t/1099345.aspx
    http://forums.asp.net/t/1199709.aspx
    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Set Focus after Postback (asp.net/vb.net newbie)

    04-20-2008, 11:34 PM

    The solution what I provided, whther it is helpful to you

    Please mark it is as answer if it is helpful to you

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Set Focus after Postback (asp.net/vb.net newbie)

    04-21-2008, 10:15 AM
    • Loading...
    • MelissaL
    • Joined on 11-02-2007, 1:32 PM
    • Anoka, MN
    • Posts 6

    Can you post the entire Javascript function please?

    ------------
    Melissa
Page 1 of 1 (6 items)