Hi,

I recently worked on an ASP.NET control that had an update panel in it and we 
put the control into a custom field type in MOSS. The code I used was similar 
to what you have, with a couple of exceptions:


*         In the OnInit event of the control is where I create the button and 
wire up the event, not in CreateChildControls

*         In CreateChildControls I add the script manager like this:

ScriptManager sm;
if (ScriptManager.GetCurrent(this.Page) != null)
{
    sm = ScriptManager.GetCurrent(this.Page);
}
else
{
    sm = new ScriptManager();
    Controls.Add(sm);
}

I don't add the ScriptManager to the master pages controls, I just slip it onto 
the page when I create the other child controls and this seems to work fine

I think you will find that if you move the code that wires up your event to the 
OnInit event it will help. The CreateChildControls method will actually run on 
every post back (I'm pretty sure anyway, I can't remember exactly), but your 
event needs to be wired up before that happens which is why you are seeing 
CreateChildControls running instead of you button click handler - what actually 
should be happening is they should both run

Brian Farnhill
L2 Consultant
Technical Consulting

Mobile:   0408 289 303
Fax:      02 8001 7778
Email:    [EMAIL PROTECTED]<mailto:[EMAIL PROTECTED]>
Web:     www.uniqueworld.net<http://www.uniqueworld.net>
Blog:     pointstoshare.spaces.live.com<http://pointstoshare.spaces.live.com/>

[cid:image001.jpg@01C8F700.0CE983B0]

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Sharepoint 
Consultant
Sent: Tuesday, 5 August 2008 1:23 PM
To: listserver@ozmoss.com
Subject: [OzMOSS] Ajax webpart - please help

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<http://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



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

Powered by mailenable.com

<<inline: image001.jpg>>

Reply via email to