Calling parent container' method

  • Thread starter Thread starter Danny Ni
  • Start date Start date
D

Danny Ni

Hi,

I have a web form A that contains an user control B, which contains an user
control C. Inside user control C, can I call methods in user control B and
web form A? If yes, How? The mthods are declared as public, of course.

TIA
 
You can, by referencing the Parent properties, i.e:

((UserControlB)Parent).DoSomething();
or
((WebFormA)Parent.Parent).DoSomething();

User control C's Parent property should reference user control B,
since C is placed on B. User control B's Parent property should
reference the form it was placed on.

You'll have to coherce the reference to the proper type, becase the
Parent property returns a Control reference (the above uses C# type
casting).

The caveat is that you'll only be able to use user control B on web
form A because it has this cast. If you put the control on any other
page that cast will fail.
 
Back
Top