asp.nethttpcontextloadcontrol

LoadControl from windows application


Is it possible to LoadControl in windows application?

I have email generation as web, but I want to move it to windows service for monthly newsletter.

Emails now are implemented as UserControls, in this way html person can easily modify look & feel.

Current rendering implementation looks like:

StringBuilder sb = new StringBuilder(4000);
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);

Page page = new Page();

EmailTemplateBase emailCtrl = (EmailTemplateBase)page.LoadControl(
                "Controls/EmailTempaltes/Template.ascx");
// Exception here

emailCtrl.DataContext = dataContext;
emailCtrl.Parameter = parameter;
emailCtrl.RenderMode = renderMode;
emailCtrl.DataBind();
emailCtrl.RenderControl(htw);

subject = emailCtrl.Subject;

string MessageText = sb.ToString().Replace("\t", "").Replace(Environment.NewLine, "");

return MessageText;

Solution

  • Solution for me was to call web service, which was able to generate Html from .ascx.

    1. Pass necessary parameters to webservice
    2. in Webservice, Build new Page, than LoadControl
    3. Set all parameters
    4. Do rendering to StringBuilder.

    You can see code in question.