OnClick事件在WebPart中不起作用

时间:2012-10-02 07:48:34

标签: c# sharepoint web-parts

我有一个简单的webpart,代码如下,但是当我点击“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 #
}

1 个答案:

答案 0 :(得分: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()));
        }

    } //