Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I've created a usercontrol that loads a list of linkbuttons from the database using a function, the linkbuttons get added to the usercontrol from the code-behind. The weird thing is that when i click the linkbutton the Click event doesn't get fired.

Any suggestions?

public partial class RapportRowControl : System.Web.UI.UserControl
{
    // Gets fired from the Page_Load of the owning WebPage. 
    public void LoadByRapportRow(tblRapportRow row) 
    {
        PlaceHolder1.Controls.Clear();

        HanDataContext dc = new HanDataContext();

        var list = from a in dc.tblCategories
                   where a.lng_prf_ID == row.tblRapport.lng_prf_ID
                   select a;

        Table t = new Table();

        foreach (var item in list)
        {
            LinkButton CategorieLink = new LinkButton();
            CategorieLink.EnableViewState = true;
            CategorieLink.ID = item.lng_ctg_ID.ToString();
            CategorieLink.CommandName = item.lng_ctg_ID.ToString();
            CategorieLink.Text = item.str_ctg_Naam;
            CategorieLink.Click += new EventHandler(CategorieLink_Click);

            TableRow tr = new TableRow();
            TableCell td = new TableCell();

            td.Controls.Add(CategorieLink);
            tr.Cells.Add(td);
            t.Rows.Add(tr);
        }

        PlaceHolder1.Controls.Add(t);
    }

    // ===== DOESN'T FIRE AT ALL =====
    private void CategorieLink_Click(object sender, EventArgs e)
    {
        ActiveRow.lng_ctg_ID = Convert.ToInt32(((LinkButton)sender).CommandName);
    }
    // ===== DOESN'T FIRE AT ALL =====
}
Posted
Comments
Dean Oliver 24-Feb-12 10:24am    
Why don't you use jQuery to fire the events based on id. Clearly your events aren't hooked up.
willempipi 24-Feb-12 10:39am    
Could you maybe point me a little more in the right direction? I've always let the .NET framework handle that stuff for me.

Hi,

Make sure that the linkbutton is re-created when it is postback otherwise the button event will not get fired.
That means the page using your control should f.x. call LoadByRapportRow (or some other function) to re-create the link button. This you can do from the the Page_Load event.

Regards
Joachim
 
Share this answer
 
Comments
willempipi 25-Feb-12 5:03am    
This worked! Does this mean I should have like a "static" view of the page ready everytime the page_init or load gets called? I used to think this was managed by .NET...
Joachim1234 25-Feb-12 19:10pm    
Well...I would create a function which creates the report on every postback. If you would like to use viewstate to maintain values etc. it's important to be aware of when the controls are added to the controls collection of the page. You can read more about that here:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx

Here's also an artcle on how to work with dynamically created controls:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=135

Regards
Joachim
Hi,
Try to call your usercontrol LoadByRapportRow method from Page_Init instead of Page_Load...
If this dont help post some code from your asp.net webpage and then we'll try to figure out where is the problem!
 
Share this answer
 
Comments
willempipi 25-Feb-12 5:05am    
Thanks for your answer, I already tried changing the page_load to page_init, but without results. The answer Joachim1234 posted seems to be the correct one, although I trying to find out why.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900