当从其他 Webparts 继承时,Webparts 中不会触发按钮 Click 事件 [英] Button Click event not getting fired in Webparts when inherited from other webparts

查看:40
本文介绍了当从其他 Webparts 继承时,Webparts 中不会触发按钮 Click 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个 Webpart,即 WebPart-1.dwp、Webpart-2.dwp 和 WebPart-3.dwp.

I have 3 Webpart namely WebPart-1.dwp, Webpart-2.dwp and 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 上按钮的单击事件.

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.

我的 WebPart-3 代码如下所示:

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.

推荐答案

问题是您的事件处理程序直到太晚才连接起来.

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

除非您自己调用EnsureChildControls,否则不会在渲染之前调用CreateChildControls,这对于按钮触发它的事件来说为时已晚.

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.

一个简单的解决方案是在 OnLoad 中调用 ensureChildControls

An easy solution is to call EnsureChildControls in OnLoad

这篇关于当从其他 Webparts 继承时,Webparts 中不会触发按钮 Click 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆