开发者

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开发者_JS百科 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.


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 );
    }
}


ToolkitScriptManager1.RegisterAsyncPostBackControl(ibtnShowPicker) you have to put above code page_load ibtnShowPicker is the id name

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜