How to return a change to a dynamic textbox

Coreysan 1,631 Reputation points
2021-10-07T21:28:17.877+00:00

I have two dynamically created text boxes, I'll call "txt1000" and "txt2000".
Here's what I can do so far:

  1. Type in a code in txt1000.
  2. Press <enter> and get to the method for event TextChanged.
  3. Use the code to get a description of the code.

How can I now do the following:

  1. Send the description to txt2000.

Can that be done? I can use FindControl to focus on "txt2000", but I have no idea how to fill the text property of it!

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

Accepted answer
  1. Yijing Sun-MSFT 7,066 Reputation points
    2021-10-08T03:31:46.513+00:00

    Hi @Coreysan ,
    If you directly find the id is "txt2000",you could pass the value from txt1000 to txt2000.Just like this:

     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);  
                form1.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;  
            }  
    

    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 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful