C# asp.net Button_Click event handler not working

Rik Van Gulck picture Rik Van Gulck · Mar 15, 2011 · Viewed 7.4k times · Source

I have a webusercontrol with a few controls on it like some labels,a textbox and eventually a button. The purpose of this control is to add it to my main page in a placeholder every time I click on the button on the webusercontrol.

This is the code behind my button on my webcontrol

  protected void btnCriteriaToevoegen_Click(object sender, EventArgs e)
  {
            //New eventhandler == all of the eventhandlers of all the objects who have                      subscribed to the event.
            EventHandler eventhandler = ButtonDoorgaan;
            ButtonOpslaanEvent mijnevent = new ButtonOpslaanEvent();

            //Basic variables I will give with my costum event(ButtonOpslaanEvent)
            mijnevent.Naam = txtCriteriumNaam.Text;
            mijnevent.Score = Convert.ToInt16(DdlCriteriumScoreSchaal.SelectedValue);
            int weging = Convert.ToInt16(DdlCriteriumWeging.SelectedValue) - 1;
            mijnevent.Weging = Convert.ToInt16(weging);

            //If the eventhandler is not null, for every object that has an eventhandler,   execute it.
            if(eventhandler!=null)
            eventhandler(sender, mijnevent);
        }

The eventhandler that need to be executed when the event is fired is defined in my main page like this :

   private void critlijn_ButtonDoorgaan(object sender, EventArgs e)
   {
       ButtonOpslaanEvent eigenevent = (ButtonOpslaanEvent)e;
       IEnumerator<Domein> domeinenumerator = domeinen.GetEnumerator();
       while (domeinenumerator.MoveNext())
       {
           if (domeinenumerator.Current.DomeinNaam.Equals(lijstdomeinitemgeselecteerd))
           {
               Criterium nieuwcriterium = new Criterium();
               nieuwcriterium.CriteriumNaam = eigenevent.Naam;
               nieuwcriterium.CriteriumScore = Convert.ToString(eigenevent.Score);
               nieuwcriterium.CriteriumWeging = Convert.ToString(eigenevent.Weging);
               domeinenumerator.Current.Criteriums.Add(nieuwcriterium);
           }
       }
       btnCriteriaToevoegen_Click(sender, e);
   }

The btnCriteriaToevoegen_Click event fires and then calls this method(addCriteriaButton()), which will add the button onto the placeholder in my main page:

private void addCriteriaButton()   
{
    Criterialijn criterialijn = (Criterialijn)LoadControl("~/Criterialijn.ascx");
    //Add eventhandlers to control
    criterialijn.ButtonDoorgaan += new EventHandler(critlijn_ButtonDoorgaan);
    criterialijn.Aangevinkt += new EventHandler(critlijn_Aangevinkt);
    //Every control on the page except this one, not enabled
    IEnumerator<Criterialijn> criterialijnenumerator = criteriacontrols.GetEnumerator();
    while (criterialijnenumerator.MoveNext())
    {
        criterialijnenumerator.Current.Enabled = false;
    }
    //Add it to a list of webusercontrols that are currently on screen
    criteriacontrols.Add(criterialijn);
    criterialijn.Enabled = true;
    //Add to placeholder
    plhCriteria.Controls.Add(criterialijn);

}

So when all this is said and done, and I run my program, he adds the control to my placeholder, but when I click on the button, he does not add a new control to my placeholder, and just clears my placeholder for some reason. Normally everything should be fine, but I have tried to see if he actually fires the event when you click on the button, and he does not. I have tried to give you a sample of my code, because the code of the whole page is quite big and that would not help you at all. Any ideas why he is not firing the event of the button?

Answer

Richard Friend picture Richard Friend · Mar 15, 2011

So when your button that you dynamically added posts back, a new page instance is created and that button no longer exists (since you only added it on the previous button click), it has not been recreated.

You must re-create dynamic controls on each postback

Remeber, a new instance of the Page class is created for each postback, any previously created controls, event handlers will not exists in the new instance unless you explicitly re-create them.