Is this possible to render page in memory?

P

Piotr Strycharz

Hi

I want to render a page in memory. That is (pseudo-code):

Page aPage = new Page();
LiteralControl lc =
new LiteralControl("<form runat=server><asp:Label id=label
Runat=server/>...");
aPage.Control.Add(lc)
aPage.RenderControl(htmlWriter);

Unfortunately, this does not work, as the page does not go through its
lifecycle (Init, Load...). So it does not know about inner controls. In fact
RenderControl does work, but the result is the text I provided to
LiteralControl because no control is instantiated - this is just a plain
text for server.

What I actually want to do is to load page template from database and render
it.

Is there a way to "run" the page in such a way as IIS does? Or: is there a
way to force Page instance to go through lifecycle?

Regards.
 
J

Jacob

Why don't you have a page that actually renders what you want to and
then send a web request to this page. Store the response stream in a
memory stream and you should be good to go.
 
B

Bruce Barker

you can create your own httpcontext and call ProcessRequest(httpcontext).

-- bruce (sqlwork.com)
 
G

George Ter-Saakov

As I am always saying everything possible - depends how much money you have
:)

In this case you can get away easy. I am using similar system for my email
templates.

It's hard to do what you want with the Page but easy with UserControl which
is pretty much the same.


Create UserControl (let say EmailTemplate).

Then in your ASP.NET page you can use following code

EmailTemplate t = (EmailTemplate)LoadControl("~/templates/mytemplate.ascx");
t.var1 = "aaaa";
t.label1 = "bbbb";
StringWriter we = new StringWriter();
HtmlTextWriter wr = new HtmlTextWriter(we);
t.RenderControl(wr);
string sHtmlText = we.ToString();

George


George
 
P

Piotr Strycharz

U¿ytkownik "Piotr Strycharz said:
I want to render a page in memory. That is (pseudo-code):

Unfortunately, almost no one has read my message carefully. The simple
RenderControl or loading it through WebRequest won't work, because Page is
initially empty and has to load its content as string. Thus no parsing
occurs and no control hierarchy is created.
I cannot do:
aPage.Controls.Add(datagrid);
which can be processed, but I have to do (pseudo):
aPage.InnerHtml = "<asp:DataGrid ID=grid Runat=server/>";

The only thing seems to be checking proposed ProcessRequest method.

Piotr.
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top