1

I have a problem similar to Calling a method in parent page from user control, except that my user control is dynamically generated at page creation.

I tried the preferred solution in the answer above, but since I don’t know in advance how many copies of the control will be required on the page, I can’t subscribe to the event on Page_Load.

This application needs a major overhaul in its logic, but it’s out of scope for my current project. I’ve currently implemented the less preferred ((MyPageName)this.Page).CustomMethod();, but would like to do it the “right” way if I can.

Do you have any suggestions?

EDIT: The code for the control is in a page separate from the page. (I intend to reuse it elsewhere in the project.) This means the control doesn't know what handlers are available on the page.

Community
  • 1
  • 1
chabzjo
  • 606
  • 1
  • 5
  • 10

1 Answers1

0

The only difference between the example in the link provided and your scenario is the context of the scope of the variable instance of the control, ultimately. That is, in their case the control is accessed using the ID specified in its declaration (say, in markup), just one, always-there, singular control.

However, in your case, you'll be conjuring up instances of the control, and have direct, immediate access to that instance; this means that, just as per the example, you can do:

// alread doing this to dynamically generate, no doubt
var control = new MyControl(); 
// then subscribe to the event of this instance...
control.MyEvent += MyEventHandler;
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • I forgot to mention I tried that too. I edited my question to show the control and the page are not in the same class. – chabzjo Mar 01 '13 at 22:47