locked
Cursor in the second textbox -VB.NET RRS feed

  • Question

  • User-1578974752 posted

    How can I show the cursor always on the second textbox of the Form when ever page load. Thanks

    Tuesday, October 29, 2019 10:45 AM

All replies

  • User753101303 posted

    Hi,

    You are using MVC or web forms? ultimately it would end up with using the https://www.w3schools.com/tags/att_input_autofocus.asp attribute.

    Tuesday, October 29, 2019 10:52 AM
  • User-1780421697 posted
    FROM CLIENT SIDE:
    ----------------
    
    focus first texbox 
    $("input:text:visible:first").focus();
    
    // second textbox as array index is 1
    $('input[@type="text"]')[1].focus(); 
    
    //By Id:
    $("#txtBox").focus();
    
    //In HTML 5:
    <input type="text" name="some_field" autofocus>
    
    Like:
    <head>
        <script src="load jquery here from CDN or local" type="text/javascript"></script>
        <script type="text/javascript">
            $(function() {
                $("#txtBox").focus();
            });
        </script>
    </head>
    <body>
        <input type="text" id="txtBox">
    </body>
    



    Tuesday, October 29, 2019 12:20 PM
  • User-1780421697 posted

    Server side:

    //Onserver side in page load method, Textbox2 is control name
    
     this.Page.Form.DefaultFocus = Textbox2.ClientID;

    Tuesday, October 29, 2019 12:28 PM
  • User288213138 posted

    Hi shsu,

    How can I show the cursor always on the second textbox of the Form when ever page load

    You can try to use focus() method. This demo for you as a reference.

    T1:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
                T2:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    
    
    protected void Page_Load(object sender, EventArgs e)
            {
                ScriptManager.RegisterStartupScript(this, typeof(Page), "FocusOnState", "document.getElementById('" + TextBox2.ClientID + "').focus();", true);
            }

    The result:

    Best regards,

    Sam

    Wednesday, October 30, 2019 3:09 AM