문제

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 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.

다른 팁

instead of the LoadControl method used in asp.net, you can use Controls.Add method of the parent control. Just add a panel to be the parent then:

UserControl uc1 = new UserControl //this is your usercontrol

Panel1.Controls.Add(uc1);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top