UserControl event never runs

N

Natan

Hi...

I have an User Control that is loaded with Page.LoadControl(). It works
fine, but the code in the .ascx file never runs. an example:

control.ascx
------------

void Page_Load(object sender, EventArgs e)
{
myDiv.InnerText = DateTime.Now.ToString("d");
}

<div id="myDiv" runat="server"></div>

page.aspx (code behind)
---------

void OnInit(EventArgs e)
{
Control c = LoadControl("control.ascx");
Controls.Add(c);
}

The control is added, but the Page_Load event of the user control is
never called. Is it right? Or it has some kind of "UserControl_Load"
instead of Page_Load?

Or should i propagate the event manually in my code?

Thanks!
 
K

Karl Seguin

I don't see anything inherently wrong with what you've shown us...

The only thing I can think of is:
Is the control's Page_Load in codebehind or inline? if it's inline, do you
have the AutoEventWireup = true in the @control directive?

Karl
 

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

Top