Why Doesn't <tab> fire TextChanged Event?

Coreysan 1,651 Reputation points
2021-10-08T21:09:41.177+00:00

In my app, I use dynamically created textboxes, as follows:

txt = new TextBox();
txt.ID = "txt_" + icntr++;
txt.Visible = true;
txt.Text = "Enter a Code";
txt.Attributes.Add("runat", "server");
txt.AutoPostBack = true;
txt.TextChanged += new System.EventHandler(this.txt_TextChanged);

In addition, I have a fixed textbox:

<asp:TextBox ID="txt_Birth" runat="server" CssClass="ShiftRight" Width="200px" AutoPostBack="true" OnTextChanged="txtBirth_TextChanged"></asp:TextBox>

I'm working on getting the app to respond to the <tab> keypress in the dynamic textboxes. That's my goal.

Scenario:

If a user enters the "txt_Birth" textbox first, and then uses the dynamic textboxes later, then the <tab> keypress will work in the dynamic textboxes.
BUT, if a user uses the dynamically created textboxes first, the <tab> keypress will not fire.

(Both have references to the autopostback).

Do you know why?

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

Accepted answer
  1. Yijing Sun-MSFT 7,071 Reputation points
    2021-10-11T07:11:29.143+00:00

    Hi @Coreysan ,
    I tried your said but I didn't reproduce your problem. Tab key is just the client operation to jump to the next control. After you tab to the fixed textbox,you need to change the textbox's value,and then it will fire the textchanged event.
    My demo codes:

    <div runat="server" id="div1">  
            </div>  
               <div>  
            <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>  
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label>  
            </div>  
    
           protected void Page_Load(object sender, EventArgs e)  
            {  
                for (int i = 1; i <=2; i++)  
                {  
                    AddControls(i);  
                }  
            }  
            private void AddControls(int controlNumber)  
            {  
                var newPanel = new Panel();  
                var newLabel = new Label();  
                var newTextbox = new TextBox();  
      
                // textbox needs a unique id to maintain state information  
                newTextbox.ID = "TextBox_" + controlNumber;  
      
                newLabel.Text = "txt"+controlNumber+"000: ";  
      
                // add the label and textbox to the panel, then add the panel to the form  
                newPanel.Controls.Add(newLabel);  
                newPanel.Controls.Add(newTextbox);  
                newTextbox.AutoPostBack = true;  
                newTextbox.TextChanged += new System.EventHandler(this.txtBox_TextChanged);  
                div1.Controls.Add(newPanel);  
            }  
            private void txtBox_TextChanged(object sender, EventArgs e)  
            {  
                var x= (sender as TextBox).Text;  
                TextBox txt2 = (TextBox)form1.FindControl("TextBox_2");  
                txt2.Text = x;  
            }  
      
            protected void TextBox1_TextChanged(object sender, EventArgs e)  
            {  
                Label1.Text="1";  
            }  
    

    Result:
    139369-animation.gif

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Coreysan 1,651 Reputation points
    2021-10-11T23:31:58.88+00:00

    @Yijing Sun-MSFT - Thank you for writing out this test. It will take me a few days to compare this to my app.
    I agree that yours works fine. But for some reason, I must have a line somewhere that makes it behave odd.

    Of course, I test by typing in a value to the dynamic textbox to signal that it has changed content, and then I hit <tab>.

    I'll see if I can get more specific in a couple of days.

    Thanks so much for your help with this - don't know what I'd do if you weren't available!!!