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.


1 Response

RE: Server side conversion of UserControl to html

R8vxBy http://yuvutu.multiply.com

- yuvutu   (http://yuvutu.multiply.com)   January 11, 2011 10:01 PM

Leave a Reply

Please enter all required information

*

*

*