koraytaylan
koraytaylan

Reputation: 893

rendering an aspx page in another one

In my web project's business object editor page, I'm sending a notification email to the administrator after an object insert or update. But instead of sending a plain text mail, i want to send the html output of another aspx page(Notification.aspx) i simply prepared for this purpose.

First i thought, I can create an instance of Notification.aspx then use it's RenderControl method for getting the output.

However, in the codebehind of Editor.aspx page, i can't even reach the Notification's reference to create a new instance.

I wonder what is the best practice for loading and rendering a page in another one...

Thanks.

Upvotes: 10

Views: 15278

Answers (6)

MartinHN
MartinHN

Reputation: 19772

You can render a page by doing this:

StringWriter _writer = new StringWriter();
HttpContext.Current.Server.Execute("MyPage.aspx", _writer);

string html = _writer.ToString();

Upvotes: 26

ygaradon
ygaradon

Reputation: 2298

this is what you looking for:

Type t = BuildManager.GetCompiledType("~/mypage.aspx");
Page p = (Page)Activator.CreateInstance(t);
p.ProcessRequest(HttpContext.Current);

from here use your imagination....

Upvotes: 0

Mark Brackett
Mark Brackett

Reputation: 85655

RenderControl won't work, because the Page won't go through it's lifecycle. I've used an HttpHandler and a Response.Filter to capture the stream in the past for a similar purpose. I've posted code over at the ASP.NET forums previously.

Edit: If you need to modify page output, you should combine this with the Server.Execute overload pointed out by MartinNH. That would simplify the code, removing the Response.Filter and such. If you just want the page output directly, MartinNH's method is very clean.

Upvotes: 0

Tor Haugen
Tor Haugen

Reputation: 19627

Sounds tricky. Keep in mind the page will need an appropriate HttpContext as well, in order to render correctly.

I would consider using a UserControl instead. These can be simply loaded and rendered with the Page.LoadControl() method. With a bit of jiggery-pokery, you can keep it from rendering in the page while extracting it's HTML.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

The page class is instantiated by the ASP.NET runtime when a request is made. So you can make a request and get the capture the response:

using (WebClient client = new WebClient())
using (Stream stream = client.OpenRead("http://mysite.com/notification.aspx"))
using (StreamReader reader = new StreamReader(stream))
{
    var contents = reader.ReadToEnd();
}

Upvotes: 0

John Rudy
John Rudy

Reputation: 37850

See this question/answer: Can I set up HTML/Email Templates in C# on ASP.NET?. Mark Brackett has what you're looking for, though there's a lot of other helpful advice there as well.

Upvotes: 0

Related Questions