【问题标题】:OnClick event doesn't work in WebPartOnClick 事件在 WebPart 中不起作用
【发布时间】:2012-09-23 01:47:03
【问题描述】:

我有一个带有以下代码的简单 Web 部件,但是当我单击“UnAcceptClick”中的按钮代码时不起作用。

我错过了什么或做错了什么?

public class simple_wp : WebPart
{

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        CreateControlHierarchy();
    }

    private void CreateControlHierarchy()
    {

                unAccept.Text = "Cancel";
                unAccept.Click += new EventHandler(UnAcceptClick);

     ... some other code ... }   


    private void UnAcceptClick(object sender, EventArgs e)
    {
        ... some code ...
    }

    protected override void CreateChildControls()
    {
        try
        {
             Controls.Add(unAccept); // button
        }
        catch (Exception ex)
        {
            Controls.Add(new LiteralControl(ex.ToString()));
        }

    } // # CreateChildControls #
}

【问题讨论】:

    标签: c# sharepoint web-parts


    【解决方案1】:

    将此添加到您的 CreateChildControls 中,您在 init 方法中关联而不是在应用程序的渲染中

    protected override void CreateChildControls()
        {
            try
            {
                 unAccept.Click += new EventHandler(UnAcceptClick);
                 Controls.Add(unAccept); // button
            }
            catch (Exception ex)
            {
                Controls.Add(new LiteralControl(ex.ToString()));
            }
    
        } //
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多