我的webpart事件处理在SharePoint中不会触发 [英] My webpart event handling does not fire in SharePoint

查看:147
本文介绍了我的webpart事件处理在SharePoint中不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始编写一些复杂的东西,然后意识到我的事件处理程序不起作用,所以我用一个事件处理程序超简化了一个按钮。请看下面的代码,也许你可以告诉我为什么它不起火?

  using System; 
使用System.Collections.Generic;
使用System.Runtime.InteropServices;
使用System.Web.UI;
使用System.Web.UI.WebControls.WebParts;
使用Microsoft.SharePoint;
使用System.Web.UI.WebControls;
命名空间PrinterSolution
{
[Guid(60e54fde-01bd-482e-9e3b-85e0e73ae33d)]
public class ManageUsers:Microsoft.SharePoint.WebPartPages.WebPart
{
按钮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;
}



//保护覆盖void OnLoad(EventArgs e)
// {
// this.EnsureChildControls();
//}

}
}


解决方案

我也有类似的问题。在我的情况下,按钮被包含在面板中,虽然父控件上的按钮正常工作,但是Panel控件没有按钮。



事实证明,需要在子面板中的 OnLoad 方法中调用 EnsureChildControls 以确保 CreateChildControls 页面的生命周期使控件可以响应事件。这一点在这个答案在这里,这是我发现我的问题的解决方案。



按照这条说明,我刚添加了以下代码到我的面板控件:

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

我注意到这个问题似乎有很多的混乱论坛,以表明这个工作我添加了跟踪语句到我的代码。以下是前后情况的结果。请注意,创建子控件调查列表的位置从PreRender事件内移动到Load事件内。



之前:





之后:




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. 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. 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.

Before:

After:

这篇关于我的webpart事件处理在SharePoint中不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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