Search code examples
c#ajaxsharepointscriptmanagercreatechildcontrols

SharePoint AJAX implementation: ScriptHandler being added twice


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.


Solution

  • 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.