How to build string with new lines and display on ASP.NET page.

R

retsam

All,
I'm working on an ASP.NET 2.0 (C#) page and have a question on how to build
a string that includes line feeds (new lines) that displays properly on an
ASP.NET page. Here's what I got:

..aspx page
<asp:Label ID="LabelMoreInformation" runat="server" Text=""></asp:Label>


code-behind for .aspx page
protected void Page_Load(object sender, EventArgs e)
{
string message = FormatMessage();

this.LabelMoreInformation.Text = message;
}



private string FormatMessage()
{
string message = "";
System.Text.StringBuilder sb = new System.Text.StringBuilder();

// Build the message
sb.Append(System.Environment.NewLine + System.Environment.NewLine);
sb.Append("Date/Time: " + System.DateTime.Now.ToString("g") +
System.Environment.NewLine);
sb.Append("System.Environment.MachineName: " +
System.Environment.MachineName + System.Environment.NewLine);

...

sb.Append(System.Environment.NewLine + System.Environment.NewLine);

message = sb.ToString();

return message;
}


The problem is that the text I put in the label does not have any line
breaks/new lines. The System.Environment.NewLine call doesn't seem to work
when I try to then display the string on an ASP.NET page. All the text is
run together.

So, how can I get line breaks/new lines for my string information in the
above scenario?
 
V

vMike

retsam said:
All,
I'm working on an ASP.NET 2.0 (C#) page and have a question on how to build
a string that includes line feeds (new lines) that displays properly on an
ASP.NET page. Here's what I got:

.aspx page
<asp:Label ID="LabelMoreInformation" runat="server" Text=""></asp:Label>


code-behind for .aspx page
protected void Page_Load(object sender, EventArgs e)
{
string message = FormatMessage();

this.LabelMoreInformation.Text = message;
}



private string FormatMessage()
{
string message = "";
System.Text.StringBuilder sb = new System.Text.StringBuilder();

// Build the message
sb.Append(System.Environment.NewLine + System.Environment.NewLine);
sb.Append("Date/Time: " + System.DateTime.Now.ToString("g") +
System.Environment.NewLine);
sb.Append("System.Environment.MachineName: " +
System.Environment.MachineName + System.Environment.NewLine);

...

sb.Append(System.Environment.NewLine + System.Environment.NewLine);

message = sb.ToString();

return message;
}


The problem is that the text I put in the label does not have any line
breaks/new lines. The System.Environment.NewLine call doesn't seem to work
when I try to then display the string on an ASP.NET page. All the text is
run together.

So, how can I get line breaks/new lines for my string information in the
above scenario?
Not to glamorous, but you could insert <br> to the string and it will result
in a carriage return.
Mike
 
S

sloan

TextBox has a multiline property somewhere.

Labels.... I think you gotta "<br/>" it like the previous post said.


You gotta remember there isn't such a thing as a Label or a TextBox on the
client side.
Its going to be rendered
<textarea> or other regular html controls.

Check your output (View Source) in the browser. Once you see how an
asp:label or asp:textbox gets rendered on the client, you'll better
understand your limitations.
 
R

retsam

Thanks vMike! That worked. Before posting the message, I had tried <br />,
but that didn't work either--it was actually displaying <br />. I guess
taking the / out made it work.
Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top