Events between ASPX and ASCX

I am new to .NET and looking from yesterday morning to solve my problem without finding a solution.

Here is my problem:

I dynamically create some user controls this way because I need to specify options:

List<ANNOUNCEMENT> listAnnouncement = getAnnoucements();
foreach(ANNOUNCEMENT ann in listAnnouncement)
{
    if(ann.IS_CURRENT_ANNOUNCEMENT && currentAnnouncement == null)
    {
         currentAnnouncement = ann;
    }
    List<Object> listParams = new List<Object>();
    listParams.Add(ann);
    AnnouncementPresentation ap = (AnnouncementPresentation)(Controller.LoadControl(Page, "~/UserControls/AnnouncementPresentation.ascx", listParams.ToArray()));
    /* important for the end of the method */
    ap.modifyAnnouncementButtonClick += new EventHandler(modifyAnnouncementButtonClick);
    pnl_announcements.Controls.Add(ap);
}

In this ASCX, I have a button, and when the user clicks on it, I want to call the method contained in my ASPX, so I do this in ASCX:

public event EventHandler modifyAnnouncementButtonClick;
protected void btn_modify_announcement_Click(object sender, EventArgs e)
{
    PageAdminAnnonces.currentAnnouncement = annonce;
    modifyAnnouncementButtonClick(sender, e);
}

And this is in ASPX:

protected void modifyAnnouncementButtonClick(object sender, EventArgs e)
{
     initListOfAnnouncement();
     lbl_errors.Text = currentAnnouncement.TITLE;
}

I think everything works, but there is a problem: it works once, and at the end of the method I delete my ASCX, as you can see, and create a new ASCX. But they have no methods, and when I click again nothing works, so ASPX reboots. After rebooting, it works again.

Am I doing something wrong?

+3
source share

All Articles