Server side conversion of UserControl to html

In order to get the html out of an ASP.NET control you can use the following code:

string pathToUserControl = "~/Controls/MyUserControl.ascx";
    
System.Web.UI.Page page = new System.Web.UI.Page();
System.Web.UI.UserControl control = page.LoadControl(pathToUserControl);
page.Controls.Add(control);
    
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);
    
string renderedHtml = writer.ToString();

This method is mostly used to send the html back to the client browser using web services.


No Responses

Leave a Reply

Please enter all required information

*

*

*