views:

12

answers:

1

I have a DataList that has a collection of People bound to it, with each Person having a Button that when clicked needs to cause an asynchronous postback so the OnClick event handler can change the details shown in an UpdatePanel [the DataList is outside of the UpdatePanel].

I have made two attempts to set the Button to change the UpdatePanel in the DataList's OnItemDataBound event handler. One assigns an AsyncPostBackTrigger to the UpdatePanel and the other was to RegisterAsyncPostBackControl to the ScriptManager. Both work but only the first time. If another Person's Button [or the same Button for a second time] is pressed the page is fully posted back.

The UpdatePanel's UpdateMode is set to Conditional and the ScriptManager has EnablePartialRenderingEnablePartialRendering set to true.

Code in the OnItemDataBound:

Button btnShowNotes = e.Item.FindControl( "btnShowNotes" ) as Button;

// Trigger
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btnShowNotes.UniqueID;
trigger.EventName = "Click";
upDetails.Triggers.Add( trigger ); // UpdatePanel

// The trigger or this is used, not both
ScriptManager1.RegisterAsyncPostBackControl( btnShowNotes );

Once the first Async PostBack has happened it seems to lose the reference, but surely this can be persisted without having to constantly rebind the DataList? I must be missing something in the way I'm attempting this.

A: 

I came up with a solution though I'm not sure it is the best/most optimal one. On each PostBack in the Page_Load method, I loop through the items and register the Aync PostBack to the buttons:

if ( IsPostBack )
{
    foreach ( DataListItem item in gvAllUsers.Items )
    {
        btnShowNotes = item.FindControl( "btnShowNotes" ) as ImageButton;
        ScriptManager1.RegisterAsyncPostBackControl( btnShowNotes );
    }
}
Rich
If someone can come up with a better way I will mark them as the answer. My own can't be for the next 11 hours anyway.
Rich

related questions