Pergunta

I have created a class that implements IHttpHandler, and I need it to return the rendered HTML of a databound user control that I have made. While I can get this to work, it doens't work exactly as I'd hoped... Basically, I make a new Page(), and add a Repeater to it, set the item template of that repeater to my user control (.ascx), set the datasource, and then databind it. Finally, I ask it to render, and then collect that HTML and send it back down the pipe.

The problem is that only the Render() methods of any controls on my user control are fired, no Init(), Load(), etc. events. Why is this? And how can I get them to fire as you would normally expect?

StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);

Page page = new Page();
Repeater rpt = new Repeater();

page.Controls.Add(rpt);

rpt.ItemTemplate = ResourceServices.LoadTemplate("mytemplate.ascx");
rpt.DataSource = datasource;
rpt.DataBind();

rpt.RenderControl(hw);

HTML = sb.ToString();
Foi útil?

Solução

You'll need to execute the page to get the events to fire. Try replacing your RenderControl line with:

context.Server.Execute(page, hw, false);

http://msdn.microsoft.com/en-us/library/ms150027.aspx

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top