Display string with whitespace in ASP.NET?
-
08-07-2019 - |
Question
I need to display a string which has a white space on a asp.net page.
****Here is what I am doing:****
cell = New TableCell
cell.Text = value (lets assume value is <" test with whitespace ">
row.Cells.Add(cell)
and it gets rendered as
<tr>
<td>" test with whitespace "</td>
</tr>
whitespaces within single quotes are not displayed.
I want this value to be displayed as it is on my page.
Solution
HTML strips out all but one space character. You need to use the &NBSP; entity to ensure white space is presented with HTML. Use the String class's Replace method (or RegEx) to swap out each space for &NBSP;
OTHER TIPS
Could it be that you try to get a space before and after your text to get some spacing between the border of the table cell and your text?
If so you should have a look at the CSS style "padding" or padding-left, padding-right. This will add space between the tables border and your text.
Add this to your page:
<style type="text/css">
td {
padding: 4px;
}
</style>
If this is not what you are after, I apologize.