Calling parent container' method

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
 
S

Scott Allen

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.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top