question

CoreyFleig-6304 avatar image
0 Votes"
CoreyFleig-6304 asked CoreyFleig-6304 answered

How to detect tab key with a dynamic textbox

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?

dotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlbertKallal-4360 avatar image
0 Votes"
AlbertKallal-4360 answered

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.




image.png (28.1 KiB)
image.png (15.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

CoreyFleig-6304 avatar image
0 Votes"
CoreyFleig-6304 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.