Click event for dynamic linkbuttons

asp.net Egypt
  • 14 years ago

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

    void Page_Load(...)
    {
       LoadFiles();
    }


    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);
    panel1.Controls.Add(butt);
    }











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

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


     

    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.

    Regards

    Brin

  • 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 http://www.developerfusion.co.uk/forums/thread/148306/

  • 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).

Contribute

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