简体   繁体   English

我的webpart事件处理不会在SharePoint中触发

[英]My webpart event handling does not fire in SharePoint

I started coding something complicated and then realized my event handlers don't work, so I super simplified a button with an event handler. 我开始编写复杂的代码然后意识到我的事件处理程序不起作用,所以我用事件处理程序超级简化了一个按钮。 Please see the code below and maybe you can tell me why it doesn't fire? 请看下面的代码,也许你可以告诉我它为什么不开火?

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
namespace PrinterSolution
{
    [Guid("60e54fde-01bd-482e-9e3b-85e0e73ae33d")]
    public class ManageUsers : Microsoft.SharePoint.WebPartPages.WebPart
    {
        Button btnNew;


        protected override void CreateChildControls()
        {
            btnNew = new Button();
            btnNew.CommandName = "New";
            btnNew.CommandArgument = "Argument";
            btnNew.Command += new CommandEventHandler(btnNew_Command);
            this.Controls.Add(btnNew);
        }

        void btnNew_Command(object sender, CommandEventArgs e)
        {
            ViewState["state"] = "newstate";
        }



        //protected override void OnLoad(EventArgs e)
        //{
        //    this.EnsureChildControls();
        //}

    }
}

I had a similar problem. 我遇到了类似的问题。 In my case the button was contained in a panel and although buttons on the parent control worked correctly the button on the child Panel control didn't. 在我的情况下,按钮包含在一个面板中,虽然父控件上的按钮工作正常,但子控件上的按钮却没有。

It turns out that you need to call EnsureChildControls in the OnLoad method in the child panel to ensure that CreateChildControls is called early enough in the life cycle of the page so that controls can respond to events. 事实证明,您需要在子面板中的OnLoad方法中调用EnsureChildControls ,以确保在页面生命周期中尽早调用CreateChildControls 以便控件可以响应事件。 This is described briefly in this answer here which is where I discovered the solution to my problem. 这个答案在这里简要描述, 是我发现问题的解决方案。

Following this instruction I just added the following code to my panel control: 按照此说明,我只是将以下代码添加到我的面板控件:

    protected override void OnLoad(EventArgs e)
    {
        EnsureChildControls();
        base.OnLoad(e);
    }

I notice that there appears to be a lot of confusion about this issue in the forums so to demonstrate that this works I added trace statements to my code. 我注意到在论坛中似乎存在很多关于这个问题的混淆,所以为了证明这是有效的,我在我的代码中添加了trace语句。 Below are the results from the before and after cases. 以下是案例之前和之后的结果。 Note that the position of Survey list creating child controls moves from within the PreRender event to within the Load event. 请注意, Survey list creating child controlsSurvey list creating child controls的位置从PreRender事件内移动到Load事件内。

Before: 之前:

在进行更改之前,在OnLoad覆盖中调用EnsureChildControls

After: 后:

进行更改后,在OnLoad覆盖中调用EnsureChildCOntrols,显示在页面生命周期中正确位置创建的子控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM