ASP.NET - Add an event handler to LinkButton inside the relay in the RenderContent call

I have a Sharepoint WebPart that loads a user control. The user control contains a repeater, which in turn contains several LinkButtons buttons.

In a call to RenderContent in Webpart, I have 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 PageNavigateButton_Click function is never called. I see that it is being added as an event handler in the debugger.

Any ideas? I'm at a dead end on how to do this.

+4
source share
6 answers

By the time of the call to RenderContent (), all handlers of the processed events were called by the infrastructure. You need to add event handlers to an earlier method, such as 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 } } 
+6
source

Did you try to assign the CommandName and CommandArgument properties to each button when iterating again? The Repeater control supports the ItemCommand event, which is the event that will occur when a control with the CommandName property is hit.

It's easy to handle from there, because CommandName and CommandArgument values ​​are passed to the event and are easily accessible.

+2
source

You need to make sure that the link button is added to the control tree and / or that the event is rewritten before the control before the event occurs.

Article @ 4guysfromrolla

+1
source

I have never done SharePoint WebPart, so I do not know if this will apply. But if it were a simple apsx page, I would say that by the time it is rendered, it's too late. Try adding event handlers to the Init or PreInit control events.


Edit: Wait, I think Dilly-O might be right. See the “Adding Control Buttons” section to the “Repeater” section at the end of http://www.ondotnet.com/pub/a/dotnet/2003/03/03/repeater.html . This is in VB.NET, but you can easily do the same in C #.

+1
source

As others noted, you added an event handler too late in the page life cycle. For SharePoint WebParts, you usually need to override the OnInit / CreateChildControls methods of the class to handle the activity.

+1
source

You need your web page to implement the INamingContainer token interface, it is used by the wireframe to allow postbacks to return to the correct control ... Also, the controls in your web part must have an identifier.

+1
source

Source: https://habr.com/ru/post/1277087/


All Articles