Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EventHandler is null

I am trying to raise a click event from User control and handle it on the containing page. The problem I have is, when I click the button 'imgstep1' on the user control, the code behind imgstep1_click event triggers and but the 'btnHandler' event is alway null. Hence it doesnt call the parent event.

Any help on this will be much appreciated.

My User Control Code is :

.ascx code:

<asp:ImageButton ImageUrl="./images/step1.gif" 
        ID="imgstep1" runat="server" 
         OnClick="imgstep1_Click"/>

.ascx.cs code :

    public delegate void OnImageButtonClick();
    public event OnImageButtonClick btnHandler;

    protected void imgstep1_Click(object sender, ImageClickEventArgs e)
    {
        if (btnHandler != null)
            btnHandler();
    }

.aspx page code:

protected void Page_Load(object sender, EventArgs e)
{
     ucStepHdr.btnHandler += new StepsHeader.OnImageButtonClick(ucStepHdr_btnHandler);
}

void ucStepHdr_btnHandler()
{
  Response.Write ('test');
}
like image 726
jack Avatar asked Feb 03 '23 09:02

jack


1 Answers

The code looks simple enough to work correctly. The only reason that btnHandler is null could be because the event registration code in the aspx page is not called.

Is there a post back ? Are you sure you are adding the event EACH TIME the page loads ???

ucStepHdr.btnHandler += new StepsHeader.OnImageButtonClick(ucStepHdr_btnHandler);
like image 152
Preets Avatar answered Feb 15 '23 13:02

Preets