LoadControl from windows application
-
28-09-2019 - |
문제
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.
- Pass necessary parameters to webservice
- in Webservice, Build new Page, than LoadControl
- Set all parameters
- 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);
제휴하지 않습니다 StackOverflow