6

In an ASP.NET 4.0 web application, I have a user control that is wrapped by an UpdatePanel (see the code below).

<asp:UpdatePanel ID="UpdatePanel5" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <UC:MyCustomCtrl ID="customCtrl" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

Obviously, this works great for every ASP.NET control that causes a postback in my user control because it makes it occur asynchronously. However, there is one process that this doesn't work for!

I have an ASP.NET button (Create Report) in the user control that makes an asychronous request to the server. The server then creates an Excel spreadsheet and then places the spreadsheet in the HttpResponse to send back to the client's browser so they can open/save it. However, it blows up at this point because the request to the server is asynchronous and apparently you can't put a binary in the HttpResponse during an asynchronous request.

How do I get around this?

Jagd
  • 7,169
  • 22
  • 74
  • 107

4 Answers4

13

Register this button as synchronous postback control in user control's Page_Load method: ScriptManager.GetCurrent(Page).RegisterPostBackControl(CreateReportButton);

Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68
  • Thanks. I arrived at a similar answer not long ago. But they're essentially the same thing - register the control with the ScriptManager. In my case it involves finding the control on the user control and then registering it with the ScriptManager. – Jagd Sep 20 '12 at 18:13
0

Make use of the triggers within the update panel to reference the custom control and an event registered on the report button within the control, e.g.

<Triggers>
     <asp:PostBackTrigger ControlID="customCtrl" EventName="ReportButtonClicked" />
</Triggers>
Turnkey
  • 9,266
  • 3
  • 27
  • 36
  • Already tried this. The button is inside of the custom control, and trying to use the name of the button as a trigger won't even compile. – Jagd Sep 20 '12 at 16:25
  • Right, will have to register an event down in the custom control and then reference it that way, will edit. – Turnkey Sep 20 '12 at 21:26
0

you can add triggers to UpdatePanels that allow full post back. here's an example

<asp:UpdatePanel ID="UpdatePanel5" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <UC:MyCustomCtrl ID="customCtrl" runat="server" />
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="btnID" />
    </Triggers>
</asp:UpdatePanel>
Eric Robinson
  • 2,025
  • 14
  • 22
  • Already tried this. The button is inside of the custom control, and trying to use the name of the button as a trigger won't even compile. – Jagd Sep 20 '12 at 16:25
  • Then is it possible take that particular user control out of the update panel? – Eric Robinson Sep 20 '12 at 16:29
0

Similar to Eric's answer. I have not tried this, but it may work...

<asp:UpdatePanel ID="UpdatePanel5" runat="server">
    <ContentTemplate>
        <UC:MyCustomCtrl ID="customCtrl" runat="server" />
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="customCtrl$btnID" />
    </Triggers>
</asp:UpdatePanel>

I did something similar to this a while back for validation controls, so it seems logical that it work here too.

Use your DOM viewer (I use Chrome's element inspector) and see what your button's "NAME" is (not ID). And starting with the portion containing the overall user control's name, use the rest.