How to generate HTML page from code?

O

Olav Tollefsen

I would like to generate a HTML page and mail it to a user. In order to
design the page, I would like to leverage the Web Form designer and
code-behind programming model, but instead of generating the page in
response to a browser request, I would like to generate the page from code
and fetch the HTML code into a string and use that as the mail body that I
send to a user by SMTP.

How can I do this?

Olav
 
L

Logan

Why not just instantiate IE & navigate to ur webpage (aspx), & after it has
been loaded,
then get hold of the HTML using the PEMS of the IE object u've got.
 
R

Roger Helliwell

I would like to generate a HTML page and mail it to a user. In order to
design the page, I would like to leverage the Web Form designer and
code-behind programming model, but instead of generating the page in
response to a browser request, I would like to generate the page from code
and fetch the HTML code into a string and use that as the mail body that I
send to a user by SMTP.

How can I do this?

Olav

Hi Olav,

You could probably use the WebClient class for this. Assuming you have
already designed a form called Mail.html with the designer, the
following code will instantiate the page on the server, and grab the
response. You can then convert it to a string, and email it out to
your clients.

using System.Net;
....
WebClient wc = new WebClient();
Byte[] pageData = wc.DownloadData("http://localhost/Mail.html");
string sHtml = Encoding.ASCII.GetString(pageData);
....
MailMessage Message = new MailMessage();
Message.Body = sHtml;

I hope that gets you started,
Roger
 
M

Matt Berther

Hello Olav,

You can do something like:

public string GetHtml()
{
MyAspx aspx = new MyAspx();
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
using (Html32TextWriter writer = new Html32TextWriter(sw))
{
aspx.RenderControl(writer);
}

return sb.ToString();
}

In this case, MyAspx is the class that your ASPX is.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top