0

我有一个带有以下代码的简单 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 #
}
4

1 回答 1

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()));
        }

    } //
于 2012-10-02T09:16:41.340 回答