我有3个Webpart,即WebPart-1.dwp,Webpart-2.dwp和WebPart-3.dwp。
我有以下情况:
WebPart-1 : WebPart-2 (Inheritance)
WebPart-2 : WebPArt-3 (Inheritance)
WebPart-3 : Microsoft.SharePoint.WebPartPages.WebPart (Inheritance)
如果我加载WebPart-1,所有Web部件都会正确加载,但WebPart-3上按钮的click事件不会被触发。
我的WebPart-3代码如下:
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);
}
对此的任何帮助都会非常明显。
答案 0 :(得分:1)
问题是你的事件处理程序直到太晚才被连接。
除非你自己调用EnsureChildControls,否则在渲染之前不会调用CreateChildControls,这对按钮触发它的事件来说太晚了。
一个简单的解决方案是在OnLoad中调用EnsureChildControls