How does Page_Load fire when control loaded with LoadControl??

J

just_jt

I'm trying to load a control containing a grid and save the rendered
HTML to a string for later storage to a database.
The following code nearly works, but the control's Page_Load event is
not being fired. I've tried adding the control to the Placeholder,
but it has no effect. How can I get the Page_Load for the control to
fire?

Thanks - jt
--------------------- code sample --------------------

Protected WithEvents PlaceHolder1 As
System.Web.UI.WebControls.PlaceHolder
protected withevents f2 as Fiscal2

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim rc as ReportCard
rc = new ReportCard()
rc.IdA.ID="ALL"

f2 = ctype(LoadControl("~/Fiscal2.ascx"),Fiscal2)

f2.ReportCard=rc
PlaceHolder1.Controls.Add(f2)

dim sw as StringWriter = new StringWriter()
dim htw as HtmlTextWriter = new HtmlTextWriter(sw)
f2.RenderControl(htw)
htw.Close()
dim s as String = sw.ToString()
 
B

Ben Lovell

Hi,

You must add the control being created through LoadControl to the page's
Controls collection for it's Page_Load event to occur. If you don't want to
add it to your page's controls collection, you can always call the Page_Load
event on your loaded control manually. Pass in the sender, and e arguments
your page's Page_Load event is recieving.
 

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

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top