First Of All make sure you have added following namespaces:
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Text;
and add following method to your code:
public string RenderControl(Control ctrl)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}
and change your code to following:
//StringBuilder sb = new StringBuilder();
HtmlGenericControl myDiv = new HtmlGenericControl("div");
myDiv.InnerText = "I want to change this text";
//Make sure asp.net does not add any prefix to your Id "example"
myDiv.ClientIDMode = System.Web.UI.ClientIDMode.Static;
myDiv.ID = "example";
//Examples using a literal, placeholder or a lable:
litExample.Text = RenderControl(myDiv);
phExample.Text = RenderControl(myDiv);
lblExample.Text = RenderControl(myDiv);
//Do Stuff with myDiv as typical control