Click event for dynamic linkbuttons Egypt
  • 14 years ago

    Hello this piece of  code has a problem that the click events don't fire:

    void Page_Load(...)

    void LoadFiles()
    DataTable td = GetTable();
    for(int i=0;i<td.Rows.Count;i++)
    //Some Code here
    LinkButton butt = new LinkButton();
    butt.CommandName = td.Rows[i]["id"].ToString();
    butt.Text = "Edit";
    butt.Click += new EventHandler(butt_edit);

    void butt_edit(....)
      LinkButton lb = (LinkButton)sender;

    int id = int.Parse(lb.CommandName);


    It should call the LoadEdit method when the linkbutton edit is clicked but the event doesnt fire

    So please help

  • 14 years ago

    dynamically added controls need to be recreated on page_load or int every time for their events to be persisted.



  • 14 years ago
    Button are already created on Page_Load as LoadFiles() method which create the buttons is basically called on page load, the problem is that events dont fire
  • 14 years ago

    The event will not be fired of course because every time you click the

    linkbutton, the page loaded and then call LoadFiles( ) again and you click the

    linkbutton and the page loaded again and call LoadFiles( ) ... and so on..

    So,by this way.. the event will never be fired ..

    Try to find a  way to call the Butt_edit () in the page load ..

    I'm ready to help you If you need any help,,


  • 14 years ago
    yes of course i need help !! :)
  • 14 years ago

    what you can do is.. try to check if the page is posting back or is it newly created.

    if ( !Page.IsPostBack )
         LoadFiles( );

    I did something similar here

  • 14 years ago
    Yea gr8 worked, in fact i did a similar example and worked fine, althu the 1st example was the same but didnt work, but unfortunetly i cant say why because i lost it in a disk format :((

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill Gates