سؤال

I have control with 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();
    }

When I clicked the button Click event not fires.

هل كانت مفيدة؟

المحلول

I think you must create click event in OnInit scope..

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

UPDATE:

First, base class of the control must be WebControl class.

And also OnInit scope define button's event:

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

Hope this helps..

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top