3
votes

I'm still stuck... I'm using ASP.NET Web Forms.

I have a TextBox:

<p><asp:TextBox runat="server" TextMode="Multiline" ReadOnly="true" ID="txtBoxAlgo" rows="50" width="878px"></asp:TextBox></p>

I can only add text to this textbox Once from the code behind:

txtBoxAlgo.Text = ("My first line of text" + Environment.NewLine);

Now my issue is if I try to add text again to the textbox nothing happens. For example:

txtBoxAlgo.Text = ("First try! It works!");
txtBoxAlgo.Text = ("Nothing, not working :(");

Now I'm moving over a simple program I have made for a C# console and C# Windows forms. I'm experimenting with ASP.NET Web Forms.

I used Console.WriteLine(""); throughout my console application. The only way I can do something similar for webforms is make Labels and TextBoxes+Multilines for each instance I wanto output text to the screen, which at this point is not worth doing all that effort for each instance of Console.WriteLine.

I tried Response.Write, but first off this shows in the top left corner of the webpage and its all unformatted.

I have ran out of ideas and I can't find anything while searching. Can you add or Append text to a textbox? Can we use Response.Write in some type of way to format everything so a user can read it?

1
By nothing happens, you mean the text doesn't change from "My first line of text", right? How are you attempting to change the text change after that? With a button? Also, by adding text again you mean, concatenate or replace? Because Text = "" is replacingFenrir88
Doesn't ReadOnly="true" have something to do with it?Quantic
The TextBox stays: "First ty! It works!". even after the second line of code that is suppose to add or change to "Nothing, not working:(" ReadOnly="false" fixed the update issue, my problem still is the textbox now only shows the last Text = ""; Is there anyway to add text to a new line while keeping the old text?702cs
@702cs i noticed this same issue with a question you asked yesterday. when adding additional text to a Textbox, you have to use Textbox1.Text += "Additional Text"; ..terbubbs
@702cs or you can use StringBuilder just like I showed yesterdayterbubbs

1 Answers

4
votes

Use += instead of = when appending extra text to your Textbox

txtBoxAlgo.Text = "My first line of text" + Environment.NewLine;
txtBoxAlgo.Text += "First try! It works!";
txtBoxAlgo.Text += "Should work.";

Every time you use = when assigning a value to your Textbox, you are just replacing the previous value which is why only the last string is visible.

Or use StringBuilder:

StringBuilder sb = new StringBuilder();
sb.AppendLine("First Line");
sb.AppendLine("Second Line");
txtBoxAlgo.Text = sb.ToString();