1

I am hoping that someone can help with the following scenario. Basically the error I am getting is that when I click on my dynamicly created button it is not firing its corresponding function.

The scenario is as follows. It is a visual webpart, but on page load a whole bunch of controls are dynamically created and added to a placeholder. Everything is loading fine, but when I click on the button control that was dynamically added it posts back, but doesnt hit my function and all my controls dissapear too, even though they set to be recreated everytime on page load (not checking for IsPostBack).

The code for adding the button is as follows:

Button _imageButton = new Button();
_imageButton.Click += new EventHandler(this.btn_ExpandCollapse);
_imageButton.CausesValidation = false;
_imageButton.ID = "m_img" + ItemId;
_imageButton.CssClass = "PlusImageButton";
_imageButton.CommandArgument = ItemId;

the function is as follows

protected void btn_ExpandCollapse(object sender, EventArgs e)
{
    //do stuff - get command argument and expand or collapse right one. 
}

I've tried not using "this" in the EventHandler I've tried not specifying the ID property I've tried not setting the CommandArgument property

It was originally an ImageButton, and that had the same issue, so it got changed to a normal Button thinking that that was the problem.

If I add a hardcoded button on the page, it triggers the function, but the moment I do it dynamically it doesnt work.

I used the same button creation code that was in a sandbox solution and the button and function gets run, but I suspect I have to do something extra as its a visual webpart. Do I have to add anything to viewstate extra? I've started doing it with the placeholder but so far no luck.

Does anybody have any ideas on what I need to do extra? Unfortunately I have to create the buttons dynamically as there is one for each list entry thats listed.

Any help would be much appreciated.

1
  • Thanks for posting the solution. Do you mind if we close this, as the problem seems to be localized to your specific code which was in error?
    – SPDoctor
    Commented Dec 13, 2011 at 16:10

4 Answers 4

1

Could you try adding the INamingContainer to your webpart? The postback data is serialized and the INamingContainer tells the ASP.NET engine to make this control as part of the postback. The ID does need to be set, otherwise the engine cannot match postback data when firing the events. a normal webpart should already have this though from 1 of it's base classes, but still, give it a try.

PS, opposed to what Nitin says, controls should NOT be created in the Init override but in the CreateChildControls override, that's what it's there for :-D. For more info, read up on the ASP.NET Page Life cycle:

ASP.NET Page life cycle

1
  • Wow, thank you for this. I've saved this in my favorites for future reference. My original error was based on ID's with the same name, so I started giving unique names with random numbers in to assist with all the different controls that get created. Means multiple versions of the same webparts can be used if needs be.
    – Sonja
    Commented Mar 12, 2013 at 11:28
0

Under what event are you creating your button? It has to be created appropriately during the Init event. This way they will become part of the control tree.

5
  • ok, let me have a look at the init event. currently everything is happening on page load, including the creation of the controls.
    – Sonja
    Commented Dec 12, 2011 at 10:35
  • I've created a test project and moved this functionality across and it is working as is. So something else is affecting my buttons. Will provide more details as I find out what it is.
    – Sonja
    Commented Dec 12, 2011 at 12:31
  • 1
    Also ensure the ID of the button is same across the postback when you are creating. You may want to perform a quick try and with hard coded ID for the button. Commented Dec 12, 2011 at 13:31
  • Hi Nitin, my id's are unique and stay consistant. However I found one bug sofar where two different controls ended up having the same name in the webpart. That is sorted out now, but there is still an issue so am delving deaper now.
    – Sonja
    Commented Dec 13, 2011 at 12:56
  • Hi Nitin, tx for your suggestion. Didnt use it in the end, but I learned something new in the process.
    – Sonja
    Commented Dec 13, 2011 at 15:35
0

It turns out that two of the dynamically created controls shared the same ID therefore the error. Everything is working fine now, and I didnt have to add anything in the OnInit Event.

2
  • apologies, I thought this question was closed. Will go through all my other questions now and make sure they are closed.
    – Sonja
    Commented Mar 12, 2013 at 11:31
  • Well! in my case the ID's are different for generated input control. and issue still persist. can you focus on right answer.
    – Red Swan
    Commented Apr 13, 2016 at 13:16
0

I know this is an old post but I just wanted to add an update for SharePoint 2013. I came across this post when experiencing the exact same symptoms in a SharePoint 2013 on-premis farm.

It turns out that if you are viewing the web part in the 'Web Part Preview' page (accessed by clicking on the web part name in the web part gallery) the events on buttons are not wired up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.