1

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I assume these Criteria are some sort of tree structure the user can navigate through (and hopefully arriving at the end somewhere ?).

About btnCriteriaToevoegen_Click:
Why are you defining an event inside a method?

In critlijn_ButtonDoorgaan and addCriteriaButton:
Instead of using an enumerator, just use

foreach(var control in criteriacontrols)
   control.Enabled = false;

So yeah, fair to say it's still not quite comprehensable, but it least I tried right? :)

EDIT

ok, then I have this question:

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

How sure are you that, when you do

EventHandler eventhandler = ButtonDoorgaan;

the variable "eventhandler" gets all eventhandlers attached to ButtonDoorgaan ?

EDIT 2 (the return)

See Richard Friend's answer; your control is not there anymore

2 Comments

The event i'm handing the eventhandler of my main page needs extra information, so I defined my own event(ButtonOpslaanEvent) with some propertys that get a value when he clicks the button, the main page can then use this information to create a new Criterium. The purpose of these "Criteriums" is to have a object of criterium per webusercontrol, so that I can write these in my database later with LINQ.
I could check that the variable is assigned all of the eventhandlers if it would just enter the click_event of the button and get to that line of code. I mean, it appears he does not fire it at all, like the control does not exist anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.