I have a web part that needs to examine every other web part on a given page for info. I've tried two approaches, but neither seems to be the answer I'm looking for.
First, I tried getting a reference to Parent (which returns the WebPartManager) and enumerating through it's Controls (or WebParts) collection. This almost works - it gives me every web part in the same WebPartZone as my web part. But it doesn't give me all the web parts in the other zones. Example:
WebPartManager wpm = (WebPartManager)this.Parent;
foreach (WebPart wp in wpm.WebParts)
{
Debug.WriteLine("I found " + wp.Title);
}
After some thinking I decided to try and enumerate through the Zones as well, but that yielded the same results. It only gives me the Zone I'm in for some reason. I don't understand why.
WebPartManager wpm = (WebPartManager)Parent;
foreach (WebPartZone wpz in wpm.Zones)
{
Debug.WriteLine("In Zone " + wpz.ID);
foreach (WebPart wp in wpz.WebParts)
{
Debug.WriteLine("I found " + wp.Title);
}
}
My second thought was to simply grab all the controls on the Page, and loop through them looking for web parts. There are two problems with this. First, we use a Site.Master as well as web parts, so the only control in the Page.Controls collection is Site.Master. Second, I want my web part to function in any given web page, so I don't want to hard-code something to work around my particular layout. So enumerating the Page controls is out.
Can anyone clue me in as to how I can enumerate all Web Parts in all zones - from a web part?
-Todd Davis
http://www.SeaburyDesign.com