views:

408

answers:

2

I have 3 Webpart namely WebPart-1.dwp, Webpart-2.dwp and WebPart-3.dwp.

I have the following scenario:

WebPart-1 : WebPart-2 (Inheritance) 
WebPart-2 : WebPArt-3 (Inheritance) 
WebPart-3 : Microsoft.SharePoint.WebPartPages.WebPart (Inheritance) 

If I load WebPart-1, all the web parts gets loaded correctly but the click event of the button on the WebPart-3 doesn't get fired.

My WebPart-3 code looks like:

protected override void CreateChildControls()
{
               base.CreateChildControls();

                m_MessageLabel = new Label();
                m_MessageLabel.ID = "m_MessageLabel";
                m_MessageLabel.Text = "Updated the settings successfully. You can now close this WebPart.";
                m_MessageLabel.Visible = false;

                m_UpdateButton = new Button();
                m_UpdateButton.ID = "m_UpdateButton";
                m_UpdateButton.Text = "Update";
                m_UpdateButton.CausesValidation = true;
                m_UpdateButton.ValidationGroup = ValidationGroup;
                m_UpdateButton.Click += new EventHandler(m_UpdateButton_Click);
}

void m_UpdateButton_Click(object sender, EventArgs e)
{
            //Some code here
}

protected override void RenderWebPart(HtmlTextWriter output)
{
                base.RenderWebPart(output);
                m_MessageLabel .RenderControl(output);
                m_UpdateButton.RenderControl(output);

}

Any help on this would be greatly appreciable.

A: 

Hi Nagendra

The problem is that your eventhandler isn't hooked up until too late.

Unless you call EnsureChildControls yourself CreateChildControls aren't called before render and this is way too late for the button to fire it's event.

An easy solution is to call EnsureChildControls in OnLoad

Per Jakobsen
I have the following code in OnLoad method: if (!Page.IsPostBack) { EnsureChildControls(); }Do you want me to remove the Page.IsPostback check?
Nagendra
Yes you need to hook up the eventhandler on every postback
Per Jakobsen
A: 

Hi, I have a similar problem (the only difference is that I'm using a UserControl), but the EnsureChildControls didn't work for me...

How did you solve this issue?

Joel Ferreras