问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/2073282/button-click-event-not-getting-fired-in-webparts-when-inherited-from-other-webpa