2

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;
st78
  • 8,028
  • 11
  • 49
  • 68
  • LoadControl of what? Why you need to loadControl in the first place to generate emails? elaborate the requirement – Illuminati Nov 26 '10 at 18:14

2 Answers2

1

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.

st78
  • 8,028
  • 11
  • 49
  • 68
0

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);
Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66
  • Microgen - you missed that part that I need it from Windows. I actually had code which do the same from Page. It works great within asp.net, but not from console application environment – st78 Nov 26 '10 at 21:17
  • i thought this is a winforms application. you cannot display GUI controls in a console application! – Ali Tarhini Nov 26 '10 at 21:21
  • This is COnsole Application and I need HTML for those WEB controls. – st78 Nov 30 '10 at 17:05