开发者

How can I render an ASP .NET user control outside of the ASP .NET runtime?

I am generating an HTML email using an ASP .NET user control, as follows

private string GetHtml(object myData)
{
    var page = new Page(开发者_如何学JAVA);
    var control = page.LoadControl("~/path/to/myEmailControl.ascx");
    page.Controls.Add(control);
    control.DataBind(myData);
    return RenderControlToHtml(control);
}

private string RenderControlToHtml(Control control)
{
    var builder = new StringBuilder();
    control.RenderControl(new HtmlTextWriter(new StringWriter(builder)));
    return builder.ToString();
}

This works fine when I use it from a web project, but recently we've added a Windows app to the solution which also needs to use this code.

Does anyone have any suggestions as to how I could get the code to work outside of the ASP .NET runtime? Once I've got an instance of a control I should be able to render it, but it's the loading of the control from the ascx file that is the problem.


You should change the control from usercontrol to customcontrol. Than you can use the render methods on the control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜