(x-post from https://stackoverflow.com/q/17065543/1943)

I have a Custom SharePoint 2010 Visual Web Part which I've realized is calling the CreateChildControls() method twice. I've tried setting the ChildControlsCreated property (it does not stay set and resets to false), checking for !Page.IsPostBack and calling Controls.Clear(), all to no avail.

Here's the code for the web part:

//using statements redacted.

namespace MyNamespace.MyWebPart
    public class MyWebPart : WebPart
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/MyNamespace/MyWebPart/MyWebPartUserControl.ascx";

        protected override void CreateChildControls()
            Controls.Clear(); //removing this line makes no difference
            Control control = Page.LoadControl(_ascxPath);
            this.ChildControlsCreated = true; //this line also makes no difference

This Web Part is on the NewForm.aspx, EditForm.aspx and DispForm.aspx pages and I've discovered that left-clicking on a list item (that will bring up EditForm.aspx with my web part in a modal window) causes the double-call, but opening the list item in a new tab will not have the issue.

Any ideas?

  • I think is a debug issue... I'm not pretty sure 100% – Salvatore Di Fazio Jun 13 '13 at 13:06
  • I've tried Debug and Release, with Debugger attached and detached - no difference. – Ryan Shripat Jun 13 '13 at 17:24
  • What about the other methods like OnLoad and OnInit? Are these methods also called twice? – banana Jun 19 '13 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.