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

Last post 06-13-2009 2:46 PM by amit_malik_in@yahoo.com. 9 replies.

Sort Posts:

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

    11-12-2008, 10:22 AM
    • Member
      point Member
    • irtaiza
    • Member since 11-11-2008, 2:40 PM
    • Posts 11

    We are migrating a Sharepoint 2003 site to MOSS 2007 and ASP.NET 2.0. We have also migrated the webparts but they are still inheriting from Sharepoint.WebPart. We have a webpart where controls are added dynamically at runtime. On this webpart a button is added dynamically and a click event handler is also attached. The button on the webpart is displayed only when there is some data. The button deletes the selected row of data when clicked. But when we click on the button, the page gets posted but the event handler is not called. We analysed and found that the code where we add the button to one of the table cells does not get executed. That means that the button is added to the webparts control collection but not to any other container's control collection and hence not added on the webpart. The button does not get displayed and event is also not handled. When we add the button forcefully to one of the container table, the event gets executed. Where are we going wrong? Or Is it an ASP.NET 2.0 limitation.

    Please note that this same code works extermely fine in ASP.NET 1.1 and Sharepoint 2003.

    Any help will be highly appreciated. Thanks.

     

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

    11-13-2008, 5:38 AM
    • Member
      128 point Member
    • JiHao
    • Member since 07-25-2007, 9:42 AM
    • Posts 30

    I didn't see the code so can't say what exactly the problem it is.

    But develop customization webpart under Sharepoint 2003 and ASP.NET is NOT same as developement under Sharepoint 2007. Under 2007 the webpart are inheriting the namespace System.Web.UI.WebControls.WebParts.

    So please read through this article http://www.wrox.com/WileyCDA/Section/Developing-SharePoint-2007-Web-Parts.id-306330.html

    Hope this one helps.

    Best Regards,

    Ji

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

    11-13-2008, 6:02 AM
    • Member
      point Member
    • irtaiza
    • Member since 11-11-2008, 2:40 PM
    • Posts 11

    Thanks Ji for the response.

    I have been able to resolve the issue. Please see my other post for the resolution. What I found out was that the code was not assigning any ID to the dynamically added controls and since some of the controls were not generated always, the ID's generated by ASP.NET were also not consistent for same control on Postback. The moment we assigned the "ID' to all the controls, everything started working fine.

     The big question before us is why is it working in the older version ASP.NET1.1 and Sharepoint 2003? Can you provide something on this?

     

    Thanks,

    Irtaiza

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

    11-13-2008, 6:22 AM
    • Member
      128 point Member
    • JiHao
    • Member since 07-25-2007, 9:42 AM
    • Posts 30

    It seems to be a typical convert ASP.NET 1.1 to ASP.NET 2.0 issue. Check these post out:

    http://www.codeproject.com/KB/aspnet/syntaxchangecontrols.aspx

    http://moustafa-arafa.blogspot.com/2007/08/loading-controls-dynamically-problem-on.html

    Hope this can help you guys get some idea.

    Regards,

    Ji

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

    11-14-2008, 1:18 AM
    • Member
      point Member
    • irtaiza
    • Member since 11-11-2008, 2:40 PM
    • Posts 11

    Thanks a lot Ji. The inputs are really helpful.

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

    11-26-2008, 5:50 AM
    • Member
      4 point Member
    • snehaltikekar
    • Member since 11-26-2008, 10:45 AM
    • Posts 2

    Hello Irtaiza. I am facing the same issue in .net 3.5. I have created a web part which has few text boxes and labels and a save button. I have assigned an id to the save button. On deploying this web part, and opening it, I am able to debug it. However, the save button click event is just not being fired. I am just stuck up with this. Please let me know if there is anything that I am missing. Thanks.

    Snehal

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

    11-27-2008, 5:08 AM
    • Member
      point Member
    • irtaiza
    • Member since 11-11-2008, 2:40 PM
    • Posts 11

    hey snehal,

    sorry for being late ..... Can you confirm that you have attached an event handler for your button. Thats somewhere in your intialization section u must have this code

    button.click += new EventHandler(button1_click);

    Make sure this is there.

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

    11-27-2008, 5:20 AM
    • Member
      4 point Member
    • snehaltikekar
    • Member since 11-26-2008, 10:45 AM
    • Posts 2

    Hello irtaiza. I have solved the issue. Actually I was accessing an object (A class in different assembly) in my event handler. On removing the code which accessed the object, my event handler started firing properly.

    I have no clue about why the event handler didn't fire earlier. I also ensured that object's assembly was strong named and installed to the GAC.Even then it didn't fire.I have worked around this problem by moving the class to the same assembly.

     

    Thanks,

    Snehal

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

    11-27-2008, 10:50 AM
    • Member
      point Member
    • irtaiza
    • Member since 11-11-2008, 2:40 PM
    • Posts 11

    That's cool!!!

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

    06-13-2009, 2:46 PM

    Hi I am facing the same problem I tried giving ID to the button but still the event is not getting fired.

    //initialize textboxes

    txtEmployeeName = new TextBox();

    txtWeekEndDate = new TextBox();

    txtClient = new TextBox();

    txtProject = new TextBox();

    txtTotalHoursWorked = new TextBox();

    //initialize label

    lblValidationError = new Label();

    lblValidationError.CssClass = "ms-formvalidation";

    lblValidationError.Visible = false;

    //initialize button

    btnSave = new Button();

    btnSave.Text = "Save";

    btnSave.ID = "buttonSave";

    btnSave.Click += new EventHandler(btnSave_Click);

    this.Controls.Add(txtEmployeeName);

    this.Controls.Add(txtWeekEndDate);

    this.Controls.Add(txtClient);

    this.Controls.Add(txtProject);

    this.Controls.Add(txtTotalHoursWorked);

    this.Controls.Add(lblValidationError);

    this.Controls.Add(btnSave);

    Please suggest

Page 1 of 1 (10 items)