views:

480

answers:

2

Whilst writing a custom webpart for use in Sharepoint 2007 / WSS 3.0 I've come across a strange problem - when a button on my webpart is clicked, the event handlers are not firing.

Following the advise MSDN, Inside Sharepoint Services 3.0 and of course stackoverflow answers, I've overridden the CreateChildControls() method of System.Web.UI.WebControls.WebParts.WebPart.

The method looks like this:

protected override void CreateChildControls()
{

    // Get the ID from the master (pseudocode here to keep it short)
    if(ICrediteurIdProviderReference == null)
    {
        // We must have the detail-id or we can't load the form
        return;
    }

    // Call base
    base.CreateChildControls();

    // ** Creation of the rest of the form removed for clarity **

    // Create button
    Button saveButton = new Button();
    saveButton.ID = "SaveButton";
    saveButton.Text = "Save";
    // saveButton.CommandName = "Save";
    // saveButton.CommandArgument = "";

    // Register event handler
    saveButton.Click += new EventHandler(saveButton_Click);

    // Add button to page
    this.Controls.Add(saveButton);

}

For clarity I've removed the initiation of the rest of the form, as well as the creation of an HTML table within which the form is placed.

My event handler looks like this:

void saveButton_Click(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("saveButton_Click fired..");
}

Yup, that's my default 'simple debug' event handler.

So far I've tried following these suggestions, but with no luck:

  • Setting CommandName and CommandArgument on the button (see commented out code)
  • Creating buttons in the OnInit() method instead of in CreateChildControls()

Starting to tear my hair out, but I assume it's something really simple that I've missed. Any help would be greatly appreciated :) :)

A: 

Corrected thanks to comments :)

It looks like CreateChildControls is called twice. The first time is before the binding of the provider values. The second time is after the binding.

For the events to fire, the controls must be added the very first time CreateChildControls is called. The state must be loaded into the controls the second tiem CreateChildControls is called (i.e. once we have the record-id from the master via ICrediteurIdProviderReference).

My new CreateClientControls() looks like this:

protected override void CreateChildControls()
{

    if (controlsLoaded == false)
    {
        LoadControls();
    }

    if (myProviderInterface != null)
    {
        LoadState(myProviderInterface.CrediteurId);
    }

}

controlsLoaded being a local boolean, which is set to true when LoadControls() has completed.

Strange how writing your problem out properly then reading it back can actually help you solve it :)

Ryan Barrett
I don't think this is correct. While it may work, the code you posted in your question should work (I just copied your code and it worked for me). I think your problem lies in the code you did not post. :)
Kit Menke
Yup, that's right. What I didn't post was the fact that CreateChildControls code would only run when it had the ConnectionProvider value.
Ryan Barrett
Corrected/similified my answer.
Ryan Barrett
+1  A: 

Your solution looks like a workaround. CreateChildControls is overidable for a reason. It allows you to add your own controls at the correct moment of the ASP.NET Control tree build / Page Life Cycle. There should be no need for calling CreateChildCOntrols in the Init Event Handler.

The probable reason for your control's event not firing is that a WebPart does not "implement" the INamingContainer interface. I put implement between quotes because it doesn't actually have any methods etc to be implemented, it is purely a so-called "marker" interface. When a control implements this interface, the ASP.NET engine marks this control and any of the controls inside as being able to generate postbacks, i.e. it gives the control a "unique id namespace in the page's control hierarchy", enabling tracking of postback events generated from that control.

Colin
Problem was that I couldn't load all the controls with their full state unless I had the ID from the master list. So the first logic in CreateChildControls went like this: if(ICrediteurProviderReference == null) { stop }That would always be true, as the binding looks to have been done at a later place.What I've done is split adding of the controls and loading of state into different methods. Then used CCC to decide when to run them (instead of making a third private method). That logic must fire OnInit, so I just call CCC. Going to make the 3rd private as it's clearer to read! ;-)
Ryan Barrett
..and of course I was going the long way around by using another event. Corrected my answer now. Thanks :-)
Ryan Barrett