4

I've got a Sharepoint WebPart which loads a custom User Control. The user control contains a Repeater which in turn contains several LinkButtons.

In the RenderContent call in the Webpart I've got some code to add event handlers:

        ArrayList nextPages = new ArrayList();
        //populate nextPages ....
        AfterPageRepeater.DataSource = nextPages;
        AfterPageRepeater.DataBind();

        foreach (Control oRepeaterControl in AfterPageRepeater.Controls)
        {
            if (oRepeaterControl is RepeaterItem)
            {
                if (oRepeaterControl.HasControls())
                {
                    foreach (Control oControl in oRepeaterControl.Controls)
                    {
                        if (oControl is LinkButton)
                        {
                            ((LinkButton)oControl).Click += new EventHandler(PageNavigateButton_Click);
                        }
                    }
                }
            }
        }

The function PageNavigateButton_Click is never called however. I can see it being added as an event handler in the debugger however.

Any ideas? I'm stumped how to do this.

Gordon Thompson
  • 4,764
  • 8
  • 48
  • 62
  • I've messed around with the code a bit after reading people's suggestions. I'm trying to add the event handlers now in the CreateChildControls() function but they still aren't being fired. Thanks for all of the help so far, it's appreciated – Gordon Thompson Sep 24 '08 at 15:49
  • 1
    For those seeing this late in the game. Don't use CreateChildControls... That's still too late in the cycle. Instead override the OnInit handler to build all your dynamic controls. – NotMe Sep 03 '09 at 01:51

6 Answers6

6

By the time RenderContent() is called, all the registered event handlers have been called by the framework. You need to add the event handlers in an earlier method, like OnLoad():

protected override void OnLoad(EventArge e)
 { base.OnLoad(e);
   EnsureChildControls();

   var linkButtons = from c in AfterPageRepeater.Controls
                                                .OfType<RepeaterItem>()
                     where c.HasControls()
                     select c into ris
                        from lb in ris.OfType<LinkButton>()
                        select lb;

   foreach(var linkButton in linkButtons)
    { linkButton.Click += PageNavigateButton_Click
    }                          
 }
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
2

Have you tried assigning the CommandName and CommandArgument properties to each button as you iterate through? The Repeater control supports the ItemCommand event, which is an event that will be raised when a control with the CommandName property is hit.

From there it is easy enough to process because the CommandName and CommandArgument values are passed into the event and are readily accessible.

Dillie-O
  • 29,277
  • 14
  • 101
  • 140
1

You need to make sure that the link button is re-added to the control tree and/or that the event is rewired up to the control before the event fires.

Article @ 4guysfromrolla

Darren Kopp
  • 76,581
  • 9
  • 79
  • 93
1

I've never done a SharePoint WebPart, so I don't know if this will apply. But if it were a plain-old apsx page, I'd say that by the time it's rendering, it's too late. Try adding the event handlers in the control's Init or PreInit events.


Edit: Wait, I think Dilli-O might be right. See the Adding Button Controls to a Repeater section at the end of http://www.ondotnet.com/pub/a/dotnet/2003/03/03/repeater.html. It's in VB.NET, but you can easily do the same thing in C#.

Matt Blaine
  • 1,976
  • 14
  • 22
1

As others have pointed out, you're adding the event handler too late in the page life cycle. For SharePoint WebParts you'd typically want to override the class' OnInit/CreateChildControls methods to handle the activity.

Preston Guillot
  • 6,493
  • 3
  • 30
  • 40
1

YOu need your webpart to implement the INamingContainer marker interface, it is used by the framework to allow postbacks to return to the correct control... Also the controls in your webpart all need to have an ID.

Colin
  • 10,630
  • 28
  • 36