본문으로 바로가기

환경 : Visual Studio 2010

예제 파일 : ContentManager.zip

출처 : http://fivewind.tistory.com/m/post/view/id/91


public static string SeverControlToHtml(Control control) { System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw); control.RenderControl(hw); return sw.ToString(); }
Boost ASP.NET performance with deferred content loading
[WebMethod]
public string GetRSSReader()
{
  // Create a new Page and add the control to it.
  Page page = new Page();
  UserControl ctl = 
    (UserControl)page.LoadControl("~/RSSReaderControl.ascx");
  page.Controls.Add(ctl);
 
  // Render the page and capture the resulting HTML.
  StringWriter writer = new StringWriter();
  HttpContext.Current.Server.Execute(page, writer, false);
 
  // Return that HTML, as a string.
  return writer.ToString();
}
웹서비스를 이용한 페이지 부분 렌더링
public class AjaxPresenterService : WebServie ...
  public string Render(string viewName) {
        string viewPath = String.Format("~/Views/{0}.ascx", viewName);
        Type type = BuildManager.GetCompiledType(viewPath);
        ViewControl control = Activator.CreateInstance(type) as ViewControl

         Page dummyPage = new Page();
         dummyPage.Controls.Add(control);

         StringBuilder response = new StringBuilder();
         StringWriter responseWriter = new StringWriter(response);
         Server.Execute(dummyPage, responseWriter, true);

         return response.ToString();
  }
  ...