usercontrol page_load not fired when programitically created

T

tracstarr

I've got a usercontrol that doesn't load properly because it's
page_load /onprerender events are not fired when I create the control
programatically. Here is the method i use to render the html for the
control. I pass in my usercontrol which is loaded as follows:

Control c1 = LoadControl("AccountDetails.ascx");

private static string RenderWithOutForm(Control ctrl)
{

Page p = new Page();
HtmlForm hf = new HtmlForm();
hf.Controls.Add(ctrl);
p.Controls.Add(hf);
//ctrl.DataBind();



System.Text.StringBuilder SB = new
System.Text.StringBuilder();
System.IO.StringWriter SW = new System.IO.StringWriter(SB);
System.Web.UI.HtmlTextWriter htmlTW = new
System.Web.UI.HtmlTextWriter(SW);
try
{
p.EnableEventValidation = false;
p.DesignerInitialize();
p.RenderControl(htmlTW);
p = null;

//now remove the form tags and viewstate div. (first 5
lines and last line)
char[] splitChar = { '\n' };
string[] strArr;
strArr = SB.ToString().Split(splitChar);
string nStr = "";

//iterate over the Array
//which contains each line of the file
for (int x = 0; x < strArr.Length; x++)
{

if ((x > 4)&&(x<(strArr.Length -1)))
{
nStr = nStr + strArr[x].ToString();
}

}

return nStr;

}



Any ideas?

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top