R – handling events of controls (ON USER CONTROL or WEB PART) added during runtime

asp.netevent handlinguser-controls

I used to have some controls added dynamically through runtime on an ASP.NET webpage and was able to handle their events but now I put the code in a user control but it doesnt work. Any idea?

    while (drr.Read())
    {
        LinkButton lnkbtnDownloadFile = new LinkButton();

        //name of the file ---> drr[2]
        lnkbtnDownloadFile.Click += new EventHandler(lnkbtnDownloadFile_Click);
        lnkbtnDownloadFile.Text = drr[2].ToString();

        PlaceHolderQuestions.Controls.Add(lnkbtnDownloadFile);
        PlaceHolderQuestions.Controls.Add(new LiteralControl("<br/>"));
    }


void lnkbtnDownloadFile_Click(object sender, EventArgs e)
{
    if (sender is LinkButton)
        DownloadFile((sender as LinkButton).Text);
}

so when i add a break point at the event handler it doesnt stop

Best Answer

I knew the reason. Because to bind the event handler with the control, the control must be drawn or initialized again, which didn't happen in my code and that's why the event wasn't fired

Related Topic