Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to always trigger an Asynchronous PostBack for an UpdatePanel from a Button in a DataList?

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.

like image 813
Rich Avatar asked Oct 25 '22 06:10

Rich


1 Answers

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 );
    }
}
like image 171
Rich Avatar answered Oct 31 '22 21:10

Rich