0

I'm using VS2010, C# to develop my ASP.NET web app, I've created a page dynamically (using several server side and HTML controls on the page). Now I'm going to send my page via email, I should have its content as HTML, how can I have my page HTML? of course I have a DIV (runat=....) which contains the parts which should be sent, how can I get its HTML content? innerHTML gives me not literal error, what are my options? I want to have the final rendered content of my DIV

thanks

2 Answers 2

3

You can render an webforms control in code using something like the following:

var control = new Control();
StringBuilder b = new StringBuilder();
HtmlTextWriter writer = new HtmlTextWriter(new System.IO.StringWriter(b));
control.RenderControl(writer);
return b.ToString();

NOTE: there is also another method to render the whole page which might be a bit easier than what has already been proposed:

var writer = new StringWriter();
HttpContext.Current.Server.Execute(url, writer);
string output = writer.GetStringBuilder().ToString();
Sign up to request clarification or add additional context in comments.

3 Comments

well if your div has a runat="server" tag that means that you can access it in your codebehind... so just replace the var control object in my code with your DIV
I've got following error: Control 'imgFa' of type 'ImageButton' must be placed inside a form tag with runat=server.
webforms need a HTML form element (hence the name) in order to work... so the control needs to be placed inside a form tag on the markup page. I would make sure that you can render everything okay in the browser before trying the code above.
1

You might want to try simply downloading the page as a string using WebClient.

var wc = new WebClient();
string url = Request.Url.AbsoluteUri;    // for the current page, otherwise set the URL
string html = wc.DownloadString(url);

Not sure if you're aware, but HTML emails are a tricky subject (email clients are like very crappy browsers, you can only use basic markup and CSS). I'd recommend checking out HTML Email Boilerplate for some tips and best practices on this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.