Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

(x-post from http://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
{
    [ToolboxItemAttribute(false)]
    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);
            Controls.Add(control);
            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?

share|improve this question
    
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
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.