1

I have been looking for any leads on this direction on the web and most of the issues I have found is when they try to add a webpart but I am running across the issue where I am getting an exception when trying to remove it. I have no trouble adding it and the web part does work once added to e webpartpage. I tried removing it and it throws an exception. I am a bit confused because my next step was to remove the code on CreateChildControls() and once I did that there was not issue with removing the web part. So its related specifically to my web part I believe. I am not quite sure what fires when a web part is deleted (related to my web parts). Just starting with all this so take it easy :)

This is what I get when I try to delete the web part:

Server Error in '/' Application.

Microsoft.SharePoint.WebPartPages.WebPartPageUserException at Microsoft.SharePoint.WebPartPages.SPWebPartManager.OnWebPartDeleting>(WebPartCancelEventArgs eventArgs) at System.Web.UI.WebControls.WebParts.WebPartManager.CloseOrDeleteWebPart(WebPart webPart, Boolean delete) at Microsoft.SharePoint.WebPartPages.SPWebPartManager.DeleteWebPartInternal(WebPart webPart) at Microsoft.SharePoint.SPWebPartCollection.Delete(Guid storageKey) at Microsoft.SharePoint.WebPartPages.WebPart.DeleteMenuOptionClick(WebPart part, Object sender, EventArgs eventArgs) at Microsoft.SharePoint.WebPartPages.MenuItem.Click(Object sender, EventArgs e) at Microsoft.SharePoint.WebPartPages.SPWebPartManager.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

private const string _ascxPath = @"~/_CONTROLTEMPLATES/[ControlLocation]";
        protected override void CreateChildControls()
        {
            try
            {
                Control control = Page.LoadControl(_ascxPath);
                Controls.Add(control);
            }
            catch (Exception ex)
            {
                Controls.Clear();
                LiteralControl control = new LiteralControl(ex.ToString());
                Controls.Add(control);
            }
        }
2
  • Can you upload the CreateChildControls() function as this sounds like the cause.
    – Simon Doy
    Commented Aug 8, 2013 at 20:49
  • I edited the original post to include the CreatedChileControl() code.
    – crossroad
    Commented Aug 9, 2013 at 17:56

1 Answer 1

0

I know this is an old post, but I ran into this same issue today. The problem I had was that I was disposing the current web in the child control.

I had a method that fetched a SPList object from a url. In doing so, I created an SPSite and an SPWeb object that were both getting disposed in a Finally block.

If the url was relative, I used SPContext.Current.Site, which was getting disposed in the Finally block. Also, the url pointed to the current web as well which was also getting disposed.

I changed the code to ensure that if the url pointed to the current SPSite or current SPWeb that they would use the SPContext.Current.Site and SPContext.Current.Web and not dispose of them in the Finally block. However, if the url pointed to a site or web outside of the current context, they would created and disposed properly.

Long story short, don't dispose of the current site and web objects. However, make sure you are disposing of SPSite and SPWeb objects when they need to be so that you don't create a memory leak.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.