ScoobyDrew18
ScoobyDrew18

Reputation: 631

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'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?

Upvotes: 0

Views: 1585

Answers (1)

ScoobyDrew18
ScoobyDrew18

Reputation: 631

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.

Upvotes: 1

Related Questions