【问题标题】:Button Click event not getting fired in Webparts when inherited from other webparts从其他 Web 部件继承时,按钮单击事件不会在 Web 部件中触发
【发布时间】:2010-07-17 04:59:03
【问题描述】:

我有 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 上按钮的单击事件。

我的 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);

}

对此的任何帮助都将非常感激。

【问题讨论】:

    标签: sharepoint sharepoint-2007


    【解决方案1】:

    问题是您的事件处理程序直到为时已晚才连接。

    除非你自己调用 EnsureChildControls CreateChildControls 在渲染之前不会被调用,这对于按钮触发它的事件来说太晚了。

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

    【讨论】:

    • 我在 OnLoad 方法中有以下代码: if (!Page.IsPostBack) { EnsureChildControls(); } 你希望我删除 Page.IsPostback 检查吗?
    • 是的,您需要在每次回发时连接事件处理程序
    猜你喜欢
    • 2015-05-20
    • 2015-03-28
    • 2020-02-20
    • 2015-12-06
    • 2012-01-19
    • 2016-04-25
    • 2012-09-12
    • 2019-06-22
    • 1970-01-01
    相关资源
    最近更新 更多