Enumerating all the web parts on a page - from a web part

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
0
toddhd
8/30/2006 7:20:59 PM
asp.net.personalization 3779 articles. 0 followers. Follow

5 Replies
796 Views

Similar Articles

[PageSpeed] 9

You can get a reference to the collection of webparts on the page be accessing WebParts property of the WebPartManager:

 WebPartCollection parts = WebPartManager1.WebParts;


David Barkol | ASP.NET AJAX in Action
0
davidbarkol
8/30/2006 10:08:07 PM

But look at the first line of both my examples - that's exactly what I'm doing.

The thing is, this works fine if I access the WebPartManager from the Page's code-behind. It returns all the web parts.

But I need to reference the WebPartManager from the Webpart. When you access the Parent of a web part, it returns a reference to the WebPartManager it resides in. But for some reason, the WPM only seems to recognize the zone from the calling web part. Try it, you'll see. Something like this:

public MyClass : WebPart

{

public void SomeFunction()

{

          WebPartManager wpm = (WebPartManager)this.Parent;
            foreach (WebPart wp in wpm.WebParts)
            {
                Debug.WriteLine("I found " + wp.Title);
            }

}

}


-Todd Davis
http://www.SeaburyDesign.com
0
toddhd
8/31/2006 12:01:59 AM
Never mind, I just setup a small test project outside of my main project, and the code posted above works just fine in the test. My main project uses MS Atlas, and I wonder if maybe that has something to do with what I'm seeing? Anyway, it works fine in my test project, so I'll have to look for what's different now.
-Todd Davis
http://www.SeaburyDesign.com
0
toddhd
8/31/2006 1:03:10 AM

Oops, sorry about that. I should have read your post more carefully. Try this:

        WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(this.Page);
        foreach (WebPart wp in wpm.WebParts)
        {
            System.Diagnostics.Debug.WriteLine("Found {0}", wp.Title);
        }

David Barkol | ASP.NET AJAX in Action
0
davidbarkol
8/31/2006 1:09:34 AM
Thanks - I've never seen the GetCurrentWebPartManager call before - I was really hoping that would do it, but the results were the same. I removed the Atlas Update Panel as well, and was hoping that might be the problem (Atlas does all kinds of funny things to web parts) but that didn't help either. I'm so confused. Clearly the syntax is correct, so I'm not sure why the results are being limited to a single zone. We have two zones on the page, with the ability to add more dynamically, but those initial two are "hard coded" into the HTML, so they should show up.
-Todd Davis
http://www.SeaburyDesign.com
0
toddhd
8/31/2006 1:33:06 AM
Reply:

Similar Artilces:

Web part Personalization and Web part custom designing
  Hi all          1. I m using a GridView control in my Web Part. In that GridView i control the columns to be displayed by generating queries in run time. In my page i have six GridView controls in six web parts. Each user can customize their GridView columns. My actual requirement is i want to get the each individual user customized details when they logged in again.How can I done this plz give me a suggestion by code.    2. I want to display my web part have a rounded corner title. how can I done this. I a...

Web part in MOSS not showing in the "Add Web Parts" web page dialog in the nested sites
hey guys,  I created a web part and it seems to work fine ( I followed  the Sahil's article: http://www.codeguru.com/csharp/.net/net_asp/webforms/article.php/c12293__1/)I added the web part using the page:_layouts/NewDwp.aspx located in the profiles site, which is http://ServerName/sites/MySite/then I asked a coworker to try the web part, he went to his profile, click in Site Actions, Edit page, Add a Web Part and he wasn't able to see it in the list, so he needed to go to Site Settings -> Web Parts -> New -> check the web part and click on Populate Gallery. S...

List web parts and web part zones from another page
Hi I'm working on an administration page that clones the content of a page within our cms. Thats the easy bit. The difficult bit is I want to be able to clone the webparts, their locations and their content that are on said page. I've been looking at the SqlPersonalizationProvider but dont think i can solve my problem by calling anything within that. I've also been looking at the webpartmanager and there does not seem to be a way of calling the methods within this class to get the content of a different page. Does anyone have any ideas how i would get the web part inform...

Sharepoint help! Error msg : "A Web Part or Web Form Control on this Web Part Page cannot be displayed or imported because it is not registered on this site as safe"
hihi, I have set up a testing virtual server on my local machine. I installed win server 2003, wss 2.0, sql server 2000, and visual studio .net 2003. i tried to create a project in C# but when i build it and import the web part (.dwp) into the page, there is this error msg "A Web Part or Web Form Control on this Web Part Page cannot be displayed or imported because it is not registered on this site as safe"  I seached most of the sites and they informed that the problem lies in the safe control in web.config i enter the safe control in web.config but i does not know where to put ...

Importing n Adding custom web parts onto a web part page
I created a custom webpart using the ASP.NET Web Control Library Template in a  C# project. Then i created a .dwp file for the web part and even edited the web.config file in the c:\inetpub\wwwroot  folder to set the safe property of the webpart to true. I then imported the .dwp file into sharepoint . Now when try to drop the web part onto a web part zone it says             "A web part or web form control on this web part page cannot be displayed or imported because it is not registered as safe on this site" What sh...

Atlas and Web Parts / Web Part Framework
How is Microsoft differentiating Atlas from Web Parts and the Web Part Framework? Which is the right one to use when? Web Parts provide a rich infrastructure in ASP.NET to build composite, personalized application UI from individual components. Atlas provides a richer environment in the browser client for application UI to run more interactively. These are complementary technologies. In the future, web parts and Atlas will work together in a couple of important ways. First, the web parts infrastructure will take advantage of Atlas to provide even richer client-side intera...

Display Web Page in Web Part
 I wonder if it's possible to display a web page in a web part control by specifying the URL only, e.g. "www.asp.net" (i.e. I want to get the same behaviour as is possible with e.g. HTML frames)? Hello,  I have a similar problem: I would like to change the page displayed in a Page Viewer webpart by clicking on a lilnk that would be on another webpart on the same page. Idealy this would be done in ASP, but javascript could be an alternative. I have not been able to get the webpart object, by its ID or by another mean, though... I use WSS 3.0 and Sharepoint Design...

Is it possible to embed part of one web page into another web page?
Any answers will be highly appreciated.  Thanks. If you can adjust the page you want to embed onto the other page, you could use a .ascx (web forms control).  Then you can add controls to it and just drag and drop it using VS.NET from the Solution Explorer window to the webform.  You can then also embed it onto other pages in that same web project easily by drag n drop.  I hope this helps, it's not exactly a portion of a web page but may be an acceptable alternative for you.Rachel AppelMVP - ASPInsider In order to better answer this, I could use some details as ...

Web part performance concern as # of available web parts increase
Perhaps I'm missing a certain element to the Web part framework. I'm slowly scaling up the number of Web parts that I have on a particular page that I have. It's not completely impossible for me to think of this page eventually having tens of Web parts available for looking at little cross sections of data in the system to provide a dashboard. Of course, a user may only have 10 or so visible. I only have four parts on my page right now within WebPartZones. What I notice is that even if my personalization settings have various WebParts configured as not to show, it runs through the data ...

Web parts: upcoming deadline for Snippets, update on Web Parts
Hi, quick update on Web parts, since the release date for Northern Spring Campaign is approaching fast. iOS won't be released on June 2nd, we'll try to reuse as many strings as possible (if not all of them) from the existing work that it will happen. *Snippets - 25 May* A few locales (ja, pl, ru) still have a pending request for snippets. Some of them are about iOS, so they won't be used in this campaign, but it's important to have them on file for the future. *Campaign - 2 Jun* The campaign is spread across several files with small updates. The removal of...

Invoke Ajax OnClick from Provider Web Part to Consumer Web Part
I am attempting to manage an ajax connection by calling a button onclick method on a separate web part in order to force the partial postback on the consumer.  Web part A (Provider) invokes the method on Web Part B (Consumer) Web Part A Type t = myButton.GetType();            object[] p = new object[1];            p[0] = EventArgs.Empty;            MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | ...

can i drag an image from one web part to another web part
hi can i drag an image from one web part to another web part i.e if i am having an image in one web part i must drag the web part and drop it in the other web part  is it possible or not pls tell me if possible give me how thanks Hi, The image control is also a standard control. WebPartManager will wrap it as a genericWebParts. What you are draging is a WebPart, not a image control. I know what you want is discarding the frame border of the WebPart control, actually you can achieve it by setting the ChromeType property of WebPart to PartChromeType.None...

Web Parts
I work for a Digital Publishing Company. The ASP.NET portal application which must allow the project managers full customization. For instance, I have a Personal Information web page which can collect all or some of the following infomration: Salutation, Title, First name, Middle Name, Last Name, Institution, Department, Street Address, city, state, postal code, country, phone, and fax number. I have designed the portal application architecture to support multiple language by using SQL Server 2005 to return all output wording based on the user's web browses langauge settings. The porta...

Same Web Part, multiple pages + pre customized part
Hi all, some issues: 1) I need to add a web part in shared mode and make it available on all pages relying the same master file (i.e. an announcements part which is in the right column) I know that i can put the part in the master file (through code), but I need to do that via "add web part" drag and drop (as an administrative taks and not as a development taks) 2) I need to build an archive of "customized parts", i.e something similar to the web part catalog, but where there are "already customized instances" of web parts. Sample: let's suppose ...

Web resources about - Enumerating all the web parts on a page - from a web part - asp.net.personalization

Not quite enumerating (iterating) enums - Scott Hanselman
Scott Hanselman on Programming, User Experience, The Zen of Computers and Life in General

Enumerating English
How many words are in the English language? 500,000? 1 million? Our linguist says there is no way to really count all the words. What's more, ...

Not quite enumerating (iterating) enums
Warning/Disclaimer: This post is very likely useless and random, providing neither prescriptive guidance nor valuable suggestion. These are the ...

Resources last updated: 12/7/2015 9:23:51 AM