DGibbs
DGibbs

Reputation: 14618

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?

Upvotes: 3

Views: 4330

Answers (3)

Barsham
Barsham

Reputation: 769

Neat solution would be:

    protected void MyRepeater_OnItemCreated(object sender, RepeaterItemEventArgs e)
    {
        //Inside ItemCreatedEvent
        ScriptManager scriptMan = ScriptManager.GetCurrent(this);
        LinkButton btn = e.Item.FindControl("btnSubmit") as LinkButton;
        if (btn != null)
        {
            btn.Click += btnSubmit_Click;
            scriptMan.RegisterAsyncPostBackControl(btn);
        }
    }

This is the source thread

Upvotes: 0

DGibbs
DGibbs

Reputation: 14618

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.

Upvotes: 8

baxter
baxter

Reputation: 42

I seem to recall that you can use the clientID rather than the uniqueID property for this.

Upvotes: 0

Related Questions