Search code examples
c#asp.netupdatepanel

Force Postback from a Button in a User Control in an Update Panel


I have a button sitting inside a control inside an update panel that is not posting back to the server and I cannot figure out why.

The button is simply an normal asp:button:

<asp:Button ID="btnContinue" runat="server" CssClass="close-reveal-modal button-primary" Text="Continue"></asp:Button>

I register its click event on load of the user control:

btnContinue.Click += new EventHandler(btnContinue_Click);

The control containing the button is the BestAvailable control on this page, in this UpdatePanel:

<asp:UpdatePanel runat="server" ID="updatePnlBestAvailable" class="best-available" ChildrenAsTriggers="true">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnReserve" EventName="Click" />
    </Triggers>
    <ContentTemplate>
        <uc:BestAvailable runat="server" ID="BestAvailableControl"></uc:BestAvailable>
        <div id="bestAvailablePanelControls">
            <asp:Button runat="server" Style="display: none;" ID="ChangeButton" 
                OnClick="Change_Click" ClientIDMode="static" 
                CssClass="ChangeButton" />
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

The postback on the button isn't occurring at all, I have a breakpoint on the first line in my click event handler and it's never hit. According to my co-workers, this is because the button is in the aforementioned update panel. I've tried several things to fix this problem without success.

  • I attempted to add the button id and a PostBackTrigger to my update panel like so from with in the user control and the containing control:

    UpdatePanelControlTrigger trigger = new PostBackTrigger();
    trigger.ControlID = this.BestAvailableControl.btnContinue.UniqueID;
    updatePnlBestAvailable.Triggers.Add(trigger);

    The result was an exception saying A control with ID '<unique id prefix>$btnContinue' could not be found for the trigger in UpdatePanel 'updatePnlBestAvailable'. I also attempted to register the control directly in the mark up with the same result.

  • I tried wrapping the button in its own update panel following this post: Custom User Control in UpdatePanel forcing Postback. This made no difference in behavior that I could see.

I'm not very familiar with update panels so I'm running out of ideas. Does anybody know why my button is not posting back to the server?


Solution

  • I figured this out a few days ago and just now realized I didn't circle back to this question. So, just in case someone ever stumbles on this, here's the deal.

    I realized that the update panel was indeed preventing the post back. Although ChildrenAsTriggers was set as true, the button being inside another user control meant that it wouldn't postback at all. I ended up creating a hidden second button directly inside the update panel and using JavaScript to make the original button click the second one, which did cause postback. Once server code was running again, I called the click event of the original button.

    Yes, it's a bit of a hack, but UpdatePanels in general are ridiculous, so you play with the hand you're dealt.