473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Correct way to add formatted HTML?

I've an object that I want to display via HTML, what is the correct way to
do this? (Or the prefered way).
Currently I'm doing it this way:
private void AddStory(Story story)

{

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append("<P><P><H2>").Append(story.Title).Append ("</H2>");

sb.Append(story.Date).Append("<BR
/>").Append(story.Category).Append("</P><P>");

sb.Append(story.Teaser).Append("</P><P>Author: <A
HREF='mailto:").Append(story.Author.Email).Append( "'>");

sb.Append(story.Author.Name).Append("</A>&nbsp;
(").Append(story.CommentsNumber()).Append(") comments");

sb.Append("&nbsp; <A
HREF='permalink.aspx?story=").Append(story.HiddenI d).Append("'>Full
story</A></P>");

Content.Controls.Add(new LiteralControl(sb.ToString()));

}

Where Content is a placeholder on the page, and the content is being added
one after another.

This is ugly and error - prone.

How do I do it in a more efficant way?

I would love to use HTML/Web controls, but I've a problem in creating the
correct Html with them.

I would appritiate it if someone could post an example how to do the above
result in a better way.
Nov 18 '05 #1
1 871
You're better off using an HTMLTextWriter than a StringBuilder. It's much
less error prone.
http://msdn.microsoft.com/library/de...classtopic.asp

Additionally, you should consider encapsulating your functionality in a web
custom control.
http://msdn.microsoft.com/library/de...ebcontrols.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com

"Ayende Rahien" <Ay****@no.spam> wrote in message
news:ej**************@TK2MSFTNGP09.phx.gbl...
I've an object that I want to display via HTML, what is the correct way to
do this? (Or the prefered way).
Currently I'm doing it this way:
private void AddStory(Story story)

{

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append("<P><P><H2>").Append(story.Title).Append ("</H2>");

sb.Append(story.Date).Append("<BR
/>").Append(story.Category).Append("</P><P>");

sb.Append(story.Teaser).Append("</P><P>Author: <A
HREF='mailto:").Append(story.Author.Email).Append( "'>");

sb.Append(story.Author.Name).Append("</A>&nbsp;
(").Append(story.CommentsNumber()).Append(") comments");

sb.Append("&nbsp; <A
HREF='permalink.aspx?story=").Append(story.HiddenI d).Append("'>Full
story</A></P>");

Content.Controls.Add(new LiteralControl(sb.ToString()));

}

Where Content is a placeholder on the page, and the content is being added
one after another.

This is ugly and error - prone.

How do I do it in a more efficant way?

I would love to use HTML/Web controls, but I've a problem in creating the
correct Html with them.

I would appritiate it if someone could post an example how to do the above
result in a better way.

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Craig Thomson | last post by:
Do people generally try and output nicely formatted html from their scripts? I have been trying to, with variable indenting for tables etc. It makes it easier to see the html and perhaps debug...
5
by: Paul Lamonby | last post by:
Hi, I am generating a HTML formatted email in PHP, but I am getting the most bizarre error, it will randomly delete characters out of the HTML code while in transit to the address, therefore...
3
by: Niel | last post by:
Hello friends, Don't know if i am posting to the right group, but if anyone has an idea about this please let me know. I just wanted to ask that is it true that it is not possible to send HTML...
4
by: Fabian | last post by:
I have a three tier nested array, used to define a map for a javascript game, and can be edited within the web page. Is there a way I can generate a visible copy of this array that I can then c&p...
3
by: Craig Petrie | last post by:
Hi, I have a large table in Word 2003 that has formatted text in the cells and wish to read and convert a cells formatted contents to html output via vb.net code. The formatting contains the...
1
by: robbiesmith79 | last post by:
Just so this is out there on the web, I battled the past 24 hours about this. Background info... I developed a ecommerce website in PHP 4 on a shared linux hosting plan from GoDaddy and had the...
4
by: cybervigilante | last post by:
I sent HTML formatted email, using PHP, to my Yahoo address from my server, and it came out fine, styles and all. I sent it to my gmail address to test it and all I see is the raw html code. But I...
2
by: Artie | last post by:
Hi, I've searched the web but can't find a solution to an apparently really simple problem. My app contains an HTML string and I need to be able to invoke the Print Dialog to print the HTML...
4
by: =?ISO-8859-1?Q?Andreas_M=FCller?= | last post by:
Hi! I'm looking for a technical primer regarding programming correct HTML newsletters. E.g. I remember reading about inline jpegs to avoid attachment problems and the like :-) Thanks for your...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.