0

我可以控制 imagebutton:

    ... 
    ImageButton btn;

    public MyControl()
    {
        btn = new ImageButton();      
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Page.RegisterRequiresPostBack(this);
    }

    protected override void CreateChildControls()
    {

        btn.Height = Height;
        btn.Width = Height;

        btn.Click += new System.Web.UI.ImageClickEventHandler(RaiseLeftClickEvent);
    }

    private void RaiseLeftClickEvent(object sender, ImageClickEventArgs e)
    {
        throw new NotImplementedException();
    }

当我单击按钮时,单击事件不会触发。

4

1 回答 1

2

我认为您必须在 OnInit 范围内创建单击事件..

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    Page.RegisterRequiresPostBack(this);
    base.CreateChildControls();
    btn.Click += new System.Web.UI.ImageClickEventHandler(RaiseLeftClickEvent);
}

更新

首先,控件的基类必须是 WebControl 类。

还有 OnInit 范围定义按钮的事件:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    Page.RegisterRequiresControlState(this);
    Controls.Clear();

    base.CreateChildControls();
    btn.Click += btnImageButton_Click;
    Controls.Add(btn);

}


void btnImageButton_Click(object sender, EventArgs e)
{
    //Handle Click event.. 
}

希望这可以帮助..

于 2013-05-15T12:00:36.020 回答