Groups | Blog | Home
all groups > asp.net > november 2004 >

asp.net : Calling parent container' method


Danny Ni
11/11/2004 10:35:33 PM
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

Scott Allen
11/12/2004 9:40:41 AM
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

[quoted text, click to view]
AddThis Social Bookmark Button