1

This is a bit of an odd question, and I'm sure there was an easier way of doing this but...

I have a method that returns a List of names. I am parsing through this list with a foreach loop. I am adding each name to one long String so that I can set the text of a Table Cell to that string. But I can't seem to get it to add a new line. Is this possible?

Here's a snippet of the code:

Earlier my table loop:

TableCell tempCell = new TableCell();

The issue:

// Returns a List of Employees on the specified day and time.
List<string> EmployeeList = CheckForEmployees(currentDay, timeCount);

string PrintedList = "";
foreach (String s in EmployeeList)
{
    PrintedList += s;
    // PrintedList += s + System.Environment.NewLine;
    // PrintedList += s + " \n";
}
tempCell.Text = PrintedList;

Both the commented code lines didn't work. Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

1

You need to append a break tag since you want the new line to show in HTML. So append <br/>. I would also recommend using a stringbuilder if it is more than a few iterations.

safetyOtter
  • 1,430
  • 14
  • 26
attila
  • 2,219
  • 1
  • 11
  • 15
1

Table cell as in HTML table cell? Do you perhaps need to use the "br" tag instead of normal newline?

Another thing, you should use StringBuilder instead of doing string concatenation the way you do. Or even better, String.Join.

string PrintedList = String.Join("br-tag", EmployeeList);

Again, not sure if the br-tag is what you are after, but prefer to use the methods in the String class when possible.

user1323245
  • 638
  • 1
  • 8
  • 20
  • Thanks for the response! It was the
    tag that I needed. Attila answered 5 minutes before you, so I gave him the check, but I had no idea String.Join was a thing, very useful!
    – titaniumshovel Mar 16 '14 at 00:09