Thankyou for this! I was wondering just exactly this thread's original question :) Aren't forums great!
I was wondering how many different ways people have performed filtering of controls to force only one instance of a web part?
The way I did it was to compare the class name of the control within the added web parts against the one being added...
protected void WebPartManager1_WebPartAdding(object sender, WebPartAddingEventArgs e)
{
// get element being added - used a base class to check each control can/not be added multiple times
MyControl addElement = (MyControl)((GenericWebPart)e.WebPart).ChildControl;
// if the control is allowed multiple times (ie: not a 'singletonpart') exit check
if (!addElement.isSingletonPart)
return;
// get the new element's control pathname
string path = addElement.GetType().Name;
// check pathname against each element currently in the document
foreach (GenericWebPart part in WebPartManager1.WebParts)
{
if (part.ChildControl.GetType().Name.Equals(path))
{
// found that the web part control has already been added, tell user and bail out of loop
singletonWebPartWarning.Visible = true;
e.Cancel = true;
break;
}
}
}
This post with no guarantees associated, being a personal submittal with no affiliation intended or implied.
<mc:WittyRemark runat="server" id="wr1" />