Hi,

I am trying to create a simple ajax webpart, however all the Microsoft
sugguestions seem to be failing.

I have installed Ajax and updated the web.config.

The issue is with the scriptmanager. When I run the sample code below I get
the following exception when adding the scripmanager to the masterpage

"The control collection cannot be modified during DataBind, Init, Load,
PreRender or Unload phases."

 If I use the following code rather than Page.Master.Page.Controls.AddAt(1,
scriptManager), I don't get an exception, but when the user clicks the
button, CreateChildControls is called rather than HandleButtonClick

            if (ScriptManager.GetCurrent(this.Page) == null)
            {
                ScriptManager Manager = new ScriptManager();
                Manager.EnablePartialRendering = true;
                this.Controls.AddAt(0, Manager);
            }

Could somone please help. I would rather add the scriptmanager dynamically
if possible.

THANKS.



SAMPLE CODE

 private Label label;
        private TextBox textBox;
        private ScriptManager scriptManager;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            // Let's find if the ScriptManager exists and add it if
not
            scriptManager = ScriptManager.GetCurrent(Page);
            if (scriptManager == null)
            {
                scriptManager = new ScriptManager();
                if (Page.Form != null)
                {
                   Page.Master.Page.Controls.AddAt(1, scriptManager)
                }
            }
        }


        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.EnsureUpdatePanelFixups();
            UpdatePanel up = new UpdatePanel();
            up.ID = "UpdatePanel1";
            up.ChildrenAsTriggers = true;
            up.UpdateMode = UpdatePanelUpdateMode.Conditional;
            this.Controls.AddAt(1, up);
            this.textBox = new TextBox();
            this.textBox.ID = "TextBox";
            up.ContentTemplateContainer.Controls.Add(this.textBox);
            this.label = new Label();
            this.label.Text = "Enter your name.";
            up.ContentTemplateContainer.Controls.Add(this.label);
            LinkButton button = new LinkButton();
            button.Text = "Say Hello";
            button.ID = "HelloButton";  // Some controls need an ID to make
them work with AJAX postbacks.
            button.Click += new EventHandler(HandleButtonClick);
            up.ContentTemplateContainer.Controls.Add(button);
        }
        private void HandleButtonClick(object sender, EventArgs eventArgs)
        {
            this.label.Text = "Hello " + this.textBox.Text;
        }
        private void EnsureUpdatePanelFixups()
        {
            if (this.Page.Form != null)
            {
                String fixupScript = @"
_spBodyOnLoadFunctionNames.push(""_initFormActionAjax"");
function _initFormActionAjax()
{
    if (_spEscapedFormAction == document.forms[0].action)
    {
        document.forms[0]._initialAction = document.forms[0].action;
    }
}
var RestoreToOriginalFormActionCore = RestoreToOriginalFormAction;
RestoreToOriginalFormAction = function()
{
    if (_spOriginalFormAction != null)
    {
        RestoreToOriginalFormActionCore();
        document.forms[0]._initialAction = document.forms[0].action;
    }
}
";
                ScriptManager.RegisterStartupScript(this,
typeof(AjaxUpdatePanelPart), "UpdatePanelFixup", fixupScript, true);
            }
        }
    }



------------------------------------------------------------------- OzMOSS.com 
- to unsubscribe from this list, send a message back to the list with 
'unsubscribe' as the subject.
Powered by mailenable.com

Reply via email to