Get Inner Html of aspx page in Asp.Net web project for email
Using ASP.NET
...
I have an email button on a popup extender
and would like to use the inner html
of another aspx
page to use for the body, then edit a couple tags before it is sent. Is this possible?开发者_如何转开发
Basically I'm using the .Net.Mail.MailMessage
to create an HtmlBody
and want to grab the html
of another page (without actually rendering the page) as opposed to recreating it in a string.
You will need to create an instance of the Page that you want the html from. After that you can use the Page's Render method (see below example about this being protected) to have it generate the html that would normally be sent to the browser to a HtmlTextWriter.
When you create the HtmlTextWriter, you can use a combination of StringBuilders and TextWriters in order to get the html from the HtmlTextWriter. Something like this.
StringBuilder SB = new StringBuilder();
StringWriter SW = new StringWriter(SB);
HtmlTextWriter htmlTW = new HtmlTextWriter(SW);
MyPage page = new MyPage(); // your page here
page.RenderHtml( htmlTW ); //You need to create this method in your page, see below
string html = SB.ToString();
So the only problem is that Page.Render is protected. You will need to expose a method in your Page class that is public that calls the Render method.
Something like this should work for that method
public void RenderHtml( HtmlTextWriter htmlTextWriter )
{
Render( htmlTextWriter );
}
Hope this helps.
First of all creating html emails doesn't allow for complicated html (internal,external styling, and other controls) see...
Html Email Guidline.
So what I did was use the following to create an html string for emailing...
1) Create a simple .htm file as a template, formatting everything in standard html tables (not asp:tables), and other standard html tags.
2) Only use in-line styling if necessary.
3) Then create replacable text words or phrases to replace. You can also use a comment to replace text or for adding to a table see markup below
<span style="font-size: 16px;">TodaysDate</span>
<table id="PeopleTable" border="1" cellpadding="3" cellspacing="0" width="720">
<tr style="font-weight: bold;">
<td width="100">
First Name
</td>
<td width="100">
Last Name
</td>
<td width="100">
Phone Number
</td>
</tr>
<!--AddRowsHere-->
</table>
4) Then from a button in code behind you need to get the .htm page as string and replace the text with values you want
Using sr As New System.IO.StreamReader(Server.MapPath("PeoplePage.htm"))
GetHtml = sr.ReadToEnd
End Using
GetHtml = Replace(GetHtml, "TodaysDate", Now.ToShortDateString)
5) Return this string to the .Net.Mail.MailMessage.Body and send it out as you normally would. Make sure to set MailMesage.IsBodyHtml = True
You could also create the entire string using StringBuilder without using a template.
If you put the content of your email into a server control, you can render it as a string.
public static string GetRenderedHtml(this Control control)
{
StringBuilder sbHtml = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(sbHtml))
using (HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter))
{
control.RenderControl(textWriter);
}
return sbHtml.ToString();
}
If you have any editable controls (TextBox, DropDownList, etc), you'll need to replace them with Labels. See the following post for a full example and explanation.
http://jrummell.com/send-a-completed-form-email-without-a-stringbuilder
精彩评论