Search code examples
c#asp.nettriggersupdatepanelpostback

Multiple update panel triggers inside repeater


I'm having some trouble creating triggers for items inside of a repeater. I would like a Linkbutton control to trigger a postback from within an update panel, I have a trigger defined in markup for a Button control which works fine:

<Triggers>
     <asp:PostBackTrigger ControlID="button" />
</Triggers>

However, I can't do this for the LinkButtons as they're created dynamically, only solution would be to add a trigger for each button in my repeaters data bound event like so:

//Inside repeater itemdatabound...
var trigger = new PostBackTrigger();
trigger.ControlID = linkButton.UniqueID;
updatepanel.Triggers.Add(trigger);

When running this code I receive an error:

A control with ID 'ctl00$content$repeater$ctl01$linkButton' could not be found for the trigger in UpdatePanel 'updatepanel'.

How can I dynamically add triggers for each of my LinkButtons?


Solution

  • Solved this. I'm assuming the reason that it didn't work in my OP is because the repeater controls aren't directly visible to the update panel.

    I suspect moving them outside of the repeater would have solved it or making a tweak to the FindControl("linkbutton") call to drill down into the repeater for the control, using this method would mean that I need to create two link button objects for each level which is undesirable.

    However, I think the cleaner solution is to register the LinkButton controls as postback controls using the scriptmanager:

    //Create triggers for each 'remove' button
    ScriptManager scriptManager = ScriptManager.GetCurrent(Page);
    if (scriptManager != null)
    {
         scriptManager .RegisterPostBackControl(linkbutton);
    }
    

    Within the repeaters OnItemDataBound event, solved it.