locked
Focus on next textbox by enter press RRS feed

  • Question

  • User855106052 posted

    I have 3 textboxs and a button. I have the focus on the first.

    How do I press the enter key to jump the focus to the second textbox and when that focus is in the second press enter and jump the focus to the third and at the end pressing the enter key jump the focus to the button and enter key execute button onclick?

                    <asp:Textbox ID="TbxDesc" runat="server"></asp:Textbox>
    
                    <asp:Textbox ID="TbxQuant" runat="server"></asp:Textbox>
    
                    <asp:Textbox ID="TbxValue" runat="server"></asp:Textbox>
    
                    <asp:LinkButton runat="server" ID="BtnSave" OnClick="Save">
                        <i class="fa fa-floppy-o" aria-hidden="true" style="margin-right:11px;"></i>&nbsp;save
                    </asp:LinkButton>   
    
    	Sub Page_Load()
                    If Not Page.IsPostBack Then
    			TbxDesc.focus
    		End If
    	End Sub
    

    Thanks for your help :)

    Saturday, November 28, 2020 4:10 PM

Answers

  • User1535942433 posted

    Hi indesk,

    Accroding to your description,do you must use code behind to implement these operations?

    I suggest it's best way that  you could javascript to tab textboxes and execute the button click event.

    Just like this:

     <script src="Scripts/jquery-1.10.2.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $("#TbxDesc").focus();
                var inputs = $(':input').keypress(function (e) {
                    if (e.which == 13) {
                        e.preventDefault();
                        var next = $(this).next();
                        if (next.prop = "input") {
                            next.focus();
                        }
                        else {
                            __doPostBack('<%=BtnSave.UniqueID%>', "");
                        }
    
                    }
                });
            });
        </script>
    <asp:TextBox ID="TbxDesc" runat="server"></asp:TextBox>
    
                <asp:TextBox ID="TbxQuant" runat="server"></asp:TextBox>
    
                <asp:TextBox ID="TbxValue" runat="server"></asp:TextBox>
    
                <asp:LinkButton runat="server" ID="BtnSave" OnClick="BtnSave_Click">
                        <i class="fa fa-floppy-o" aria-hidden="true" style="margin-right:11px;"></i>&nbsp;save
                </asp:LinkButton>
                <asp:Label runat="server" ID="lb1" Text=""></asp:Label>

    Code-behind:

     Protected Sub BtnSave_Click(sender As Object, e As EventArgs)
            lb1.Text = "1"
        End Sub

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 30, 2020 2:37 AM