0

I am currently in the process of setting up AJAX capabilities in a SharePoint environment to support web parts I'm creating with C#. I am using Microsoft's walkthrough as a starting point just to get up and running, and I am running into what I believe is a strange error. The problem is occurring in CreateChildControls():

    protected override void CreateChildControls()
    {

        base.CreateChildControls();

        //Fix for the UpdatePanel postback behaviour.
        EnsurePanelFix();

        LinkButton sayHello = new LinkButton();
        UpdatePanel refreshName = new UpdatePanel();
        ScriptManager scriptHandler = new ScriptManager();
        displayName = new Label();
        inputName = new TextBox();

        //Set up control properties.
        this.displayName.ID = "displayName";
        this.displayName.Text = "Hello!";
        this.inputName.ID = "inputName";
        sayHello.ID = "sayHello";
        sayHello.Text = "Say Hello";
        scriptHandler.ID = "scriptHandler";
        refreshName.ID = "refreshName";
        refreshName.UpdateMode = UpdatePanelUpdateMode.Conditional;
        refreshName.ChildrenAsTriggers = true;

        //Add the EventHandler to the Button.
        sayHello.Click += new EventHandler(ClickHandler);

        //Add the user interface (UI) controls to the UpdatePanel.
        refreshName.ContentTemplateContainer.Controls.Add(this.inputName);
        refreshName.ContentTemplateContainer.Controls.Add(sayHello);
        refreshName.ContentTemplateContainer.Controls.Add(this.displayName);

        //The ScriptManager control must be added first.
        this.Controls.Add(scriptHandler);
        this.Controls.Add(refreshName);
    }

The problem that I am having is that for some reason, CreateChildControls() must be called twice. When I step through the code, it executes every line with a problem, then goes back to the first line and repeats. When it gets to the second-to-last line where it adds the ScriptManager, it of course throws an exception, because it's trying to add a second ScriptManager to the page with an identical key. Perhaps it's an error in my understanding of how this method should work, but I don't understand why it's being called twice (or infinitely recursively, for all I know, if that exception weren't thrown). Thanks for any help anyone can give me.

4 Answers 4

4

I know this is already marked as answered, but this is the code that inserts the ScriptManager if one is not found in the page:


public abstract class WebPartBase : WebPart
{
    protected override void OnInit(EventArgs e)
    {
        if (ScriptManager.GetCurrent(Page) == null)
        {
            ScriptManager scriptManager = new ScriptManager();
            scriptManager.ID = "ScriptManager1";
            scriptManager.EnablePartialRendering = true;
            Page.Form.Controls.AddAt(0, scriptManager);
        }
        base.OnInit(e);
    }
}

all webparts inherit from this base class. All the other code was removed for clarity, this only does the script manager but you can handle web part errors and other glitches in there.

1

You could add a private flag at the class level - Check for it after your base.CreateChildControls() and bypass the remainder of the function if its set then set it when the scriptHandler is added, monitor in debug to make sure its not an infinite loop.

Also check the call stack - CreateChildControls() may be called from different places.

And, as a complete aside, have you tried Microsoft SharePoint Designer - I started developing web controls for SharePoint in VS 2005 with Ajax, but then found I could do most things using SharePoint Designer instead.

2
  • I have tried SharePoint Designer, and I do like it. Unfortunately, for this web part in particular, doing it by in VS will be quicker, provided this AJAX capability is up and running soon. I'll try this out and see what happens!
    – Geo Ego
    Dec 16, 2009 at 15:20
  • That did it! Thanks for your help! I'll track down where the call is coming from later, but for now, this is fine.
    – Geo Ego
    Dec 16, 2009 at 16:15
0

Make sure you are not calling CreateChildControls in your code. Use EnsureChildControls instead.

0

I had this problem, and I fixed it by setting the Control.ChildControlCreated flag to true at the end of CreateChildControls. This is basically the same as Tom Brown's answer, but built in.

you can read more here: http://msdn.microsoft.com/en-us/library/system.web.ui.control.childcontrolscreated.aspx

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.