tster
tster

Reputation: 18257

Simulate ASP.NET Page lifecycle

I have an HttpHandler in which I want to render the HTML for some custom controls.

Currently my code looks like this:

Page p = new Page();
var customControl = new CustomControl { Data = data, Blah = blah };
p.Controls.Add(customControl );
context.Response.Write(customControl.RenderToString());

The problem is that customControl (and child controls thereof) needs to do stuff in the OnInit, OnLoad and OnPreRender methods.

I have tried manually calling this methods through helper methods but I get various errors. My general problem is that I need the ASP.NET page lifecycle to run on the p variable. Is there any way to get that to work?

Upvotes: 2

Views: 448

Answers (2)

santa
santa

Reputation: 1003

i haven't come to try this yet... works for me!

based on ScottGu's work, jim suggests something like this:

HttpContext.Current.Server.Execute(myPage, myTextWriter, false);

Upvotes: 1

ChrisLively
ChrisLively

Reputation: 88092

Sure. Instead of an HttpHandler use a regular page...

Barring that, change your control to work a bit differently.

Upvotes: 0

Related Questions