How to detect tab key with a dynamic textbox

Coreysan 1,651 Reputation points
2021-10-06T16:38:24.82+00:00

I'm learning to write a web app, where I will create dynamic textboxes using classic Asp.Net in C# (Not Windows forms).
When a user enters data into a dynamic textbox and presses <enter>, it drops into a TextChanged event:

            TextBox txt = new TextBox();
            txt.ID = "txt_1000";
            txt.Visible = true;
            txt.Attributes.Add("runat", "server");
            txt.Width = 200;
            txt.Enabled = true;
            txt.TextChanged += txt_TextChanged;

However, what I'd really like is to have a user enter data into the textbox and presses <tab>, I can then capture that keystroke and
do work with the textbox data.

I've read articles online but what I found deals with Windows Form events.

How would I detect the tab key inside a TextChanged event?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,409 questions
0 comments No comments
{count} votes

Accepted answer
  1. Albert Kallal 5,231 Reputation points
    2021-10-06T20:20:58.463+00:00

    Ok, I assume this is asp.net + web forms?

    When a user hits enter in a text box, usually nothing will occur. However, if you set the text box AutoPostBack="true", then BOTH enter key, or tab will post back. In fact even if you don't change anything in the text box, the page will post back by hitting enter.

    Hitting tab will go to the next control on the web page - if you changed text, then both post-back and the text changed stub for that control fires. If you don't change the text box, enter STILL will fire a post back. Now of course if you have mutli-line text box, then enter goes to the next line in that text box (no post-back).

    So, with this?

    138240-image.png

    ,so the first text box? Yes, the server side text event does fire when you hit tab (but ONLY if you changed the text). The code is this:

        Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged  
      
            Debug.Print("user typed in text box 1 = " & TextBox1.Text)  
      
      
        End Sub  
      
    

    So if I type in this and hit tab?

    138311-image.png

    Output:

    user typed in text box 1 = This is some fun text  
      
    

    So, with autopostback = true, then yes, hitting tab will fire/trigger the code behind event stub for that text box.

    It not very much recommended to have a lot of post backs on such web pages, but adding auto-post back will then trigger the event code stub for text changed in the code behind.

    as noted, enter key does not post back as a general rule, unless you have set auto-post back = true.

    To be fair, few people now use enter key, but for a search box, it can be desired.

    So the Rosetta stone here is really if you have autopostback=true or not.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Coreysan 1,651 Reputation points
    2021-10-07T00:54:10.823+00:00

    Albert - thank you SO much - you nailed it! So cool.

    0 comments No comments