Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncPostBackTrigger in a Repeater outside UpdatePanel

I am trying to get a cbxSupplement trigger updatepanel refresh, but am not sure if I am using a wrong EventName or it is just impossible to do it with CheckBox. If I replace CheckBox with Button, it works fine.

<asp:Repeater ID="repSupplements" runat="server">
    <ItemTemplate>
        <asp:CheckBox runat="server" ID="cbxSupplement" />
    </ItemTemplate>
</asp:Repeater>
<asp:UpdatePanel runat="server" ID="up1">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="repSupplements" EventName="CheckedChanged" />
    </Triggers>
    <ContentTemplate>
        //Get checked items
    </ContentTemplate>
</asp:UpdatePanel>
like image 438
mko Avatar asked Dec 27 '22 08:12

mko


1 Answers

Since CheckBox controls inside repeater are not available at design time you should register them with ScriptManager.RegisterAsyncPostBackControl method. This method requires ScriptManager either on page or on master page.

Create a handler for Repeater.OnItemCreated event and there register newly created CheckBox. The code is following (note that CheckBox should have AutoPostBack property set to true):

<asp:Repeater ID="repSupplements" runat="server" 
    OnItemCreated="repSupplements_ItemCreated">
    <ItemTemplate>
        <asp:CheckBox runat="server" ID="cbxSupplement" AutoPostBack="True" />
    </ItemTemplate>
</asp:Repeater>

<asp:UpdatePanel runat="server" ID="up1">
    <Triggers>

    </Triggers>
    <ContentTemplate>
        //Get checked items
    </ContentTemplate>
</asp:UpdatePanel>

Codebehind:

protected void repSupplements_ItemCreated(object sender, RepeaterItemEventArgs e)
{
    var control = e.Item.FindControl("cbxSupplement");
    ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
}

This should do what you want.

like image 170
Alexander Manekovskiy Avatar answered Feb 16 '23 01:02

Alexander Manekovskiy