-->

ASP.NET - Add Event Handler to LinkButton inside o

2019-05-05 04:34发布

问题:

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.

回答1:

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
    }                          
 }


回答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.



回答3:

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



回答4:

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#.



回答5:

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.



回答6:

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.