Button click event not getting fired for a custom webpart where controls are added dynamically.

Last post 11-13-2008 5:51 AM by irtaiza. 1 replies.

Sort Posts:

  • Button click event not getting fired for a custom webpart where controls are added dynamically.

    11-12-2008, 10:59 AM
    • Member
      point Member
    • irtaiza
    • Member since 11-11-2008, 9:40 AM
    • Posts 11

     We are migrating from ASP.NET1.1/Sharepoint 2003 to ASP.NET 2.0/Moss 2007. We have a webpart on which controls are getting added dynamically. We have a button which is also dynamically added and a click event handler is also attached to it. However when we click on this button the page gets posted but the event is not fired. We analysed and found that the code to add the button in the webparts controls collection is executed but the code to add the button to it's container (that is a table) is not getting executed. Hence the event is not getting handled. If we forcefully add the button to the containers control collection, the event handler is called. However the same code runs in the old ASP.NET 1.1 and Sharepoint 2003 environment.

    Can somebpdy please help with this? Is there anything which has changed from ASP.NET1.1 to 2.0 which is causing this?

  • Re: Button click event not getting fired for a custom webpart where controls are added dynamically.

    11-13-2008, 5:51 AM
    • Member
      point Member
    • irtaiza
    • Member since 11-11-2008, 9:40 AM
    • Posts 11

    Well Thank GOD and to a dear friend with whose help we were able to resolve the issue. Thanks a ton Vijeta. You deserve a party and you will be treated soon.

    The scenario was that the button was getting added dynamically and event handlers were not getting called. What we missed out on was assigning an "ID" to the button. Since the controls were conditionally getting added to the webpart, the "ID"'s which ASP.NET assigns to the controls were not consistent. So initially the button "ID" was "_ctrl5" and on postback since some of the controls were not created the button "ID" was "_ctrl3". So we believed the event handler is not getting called because of this. We simply assigned an "ID" to the button in the code and it worked.

    We have similar webparts with many buttons in different rows but with same name. These were also getting added dynamically and since the time we have assigned them an "ID", everything is working fine.

    Sad part 1--> I have never found a mention on any of the sites about this, that you need to assign an "ID" always to a control inorder to handle it's event. I have found posts where people have experienced similar issues but no one gave the solution.

    Sad Part 2 --> The same code "one without ID's" still works on the old version, that is, ASP.NET1.1/SharePoint2003. How does this work is still a question? If It was a bug then why wasn't it ever published that such a bug is fixed in ASP.NET 2.0.

    Good Part --> We have resolved the issue and I have also provided a solution on this post for people like us who suffer because of some undocumented feature/bug.

    Will appreciate comments from people who faced or are facing this issue. Or if anyone has links to any documentation or web where anything related to this is mentioned, please provide a link.

    Thanks again, Vijeta.

    Regards,

    Irtaiza

Page 1 of 1 (2 items)