Monday, August 10, 2009

Getting HTML from ascx in a class

//namespace
using System.Text;
using System.IO;

//public class ThisClass: System.Web.UI.Page//class name should inherit from ystem.Web.UI.Page

public string ReturnContent(string username)
{
Control c = Page.LoadControl("WebUserControl.ascxx");
((UserControlBaseMemberDesign)c).function(username)//function in webusercontrol.ascx for displaying in ascx page
HtmlForm form1 = new HtmlForm();
form1.Controls.Add(c); //a control as html for class

StringWriter sw = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
string contents = null;
try
{
c.RenderControl(writer);
contents = sw.GetStringBuilder().ToString();
}
finally
{
sw.Close();
writer.Close();
}
return contents;
}

No comments:

Post a Comment