Restricting the number instances of a webpart added to a webpage

One of the attendees of the last Community Night asked me the following question, "How do we restrict the number of instaces of a webpart that are added to a webpage?"

So I sat down and worked out a little example on how to do that.

First thing I did was that I declared a private int variable to hold the number of instances of a webpart that are allowed on a page

private int count = 3;

Then I wrote an event handler for the WebPartAdding event of the WebPartManager class


    protected void WebPartManager1_WebPartAdding(object sender, WebPartAddingEventArgs e)
    {
       
        if(e.WebPart.GetType()==typeof (WMZ.CustomerIDWebPart))
            if (GetPartCount() == count)
            {
                e.Cancel = true;
                Page.ClientScript.RegisterStartupScript(this.GetType(), "k", "alert('Sorry, you can only add " + count.ToString() +" instances of the Customer ID Provider webpart');", true);
            }
           
       
    }


The first thing I'm doing in the event handler is that I'm checking to see if the webpart thats been added is of the type that I want to restrict, in this example its WMZ.CustomerIDWebPart (it's a custom webpart that I developed). Then I'm checking if the number of instances of this webpart that are already added to the page is equal to the limit that I set. GetPartCount() is a function that I wrote which returns the number of instances of the webpart on the page, we'll see it in a second.
If the webpart has already reached its limit, I'm canceling the event, and showing a message to the user telling him that he cannot add more webparts of this type to this page.

Now how does the GetPartCount() function work. Lets see the code first


     private int GetPartCount()
    {
        WebPartCollection c = this.WebPartManager1.WebParts;
        int count = 0;
        for (int i = 0; i < c.Count; i++)
           
            if (c[i].GetType() == typeof(WMZ.CustomerIDWebPart))
                if(!c[i].IsClosed)
                count++;
        return count;
    }


The first line in the function gets a reference to the WebParts collection of the WebPartManager object that's on the page. The WebParts collection contains references to all the webparts that are added to the page. Then I'm looping through the webparts, checking for the type I want to restrict, checking to see if the webpart is not closed (if the webpart is closed, it will disappear from the page. To add it again you need to use the PageCatalog part. Adding a closed webpart from the PageCatalog part will raise the WebPartAdding event, and if we have reached the limit of added webparts, it will not allow us to add it again to the page). The rest of the function is pretty simple, I'm just incrementing a counter, and then I'm returning back the value of the counter.

Happy programming!!!

Published Sunday, August 07, 2005 8:25 AM by admin
Filed Under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

What do you think?

(required) 
required 
optional
required