Button Click event not getting fired in Webparts when inherited from other webparts

孤人 提交于 2019-12-24 08:45:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!