Yes, you can attach the event handler yourself after you have added the control:
protected void Page_Load(object sender, EventArgs e)
{
Control myUserControl = (Control)LoadControl("MyControl.ascx");
PanelX.Controls.Add(myUserControl);
myUserControl.SomeEvent += new SomeEventHandler(eventHandler_Method);
}
private void eventHandler_Method(object sender, EventArgs e)
{
//your code here
}
I have a little experience with ASP.NET but if anyone more experienced wants to chime in and correct me, go ahead, but i believe you should move the control creation to the page Init event rather than the page load. If you have a look at the page lifecycle , the hardcoded controls are added during Initialization and then any values in viewstate are set during Load. By adding them in the Init event you can be sure they are present if any code tries to access them OnLoad :)
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246