locked
Using the Response object inside UpdatePanel RRS feed

  • Question

  • User1986727717 posted

    Hi, I have problem using the Response object inside the updatepanel

           <asp:UpdatePanel ID="UP_ExportPrompt" runat="server" UpdateMode="Always">
                    <ContentTemplate>
                        <div>
                            <ul>
                                <li>
                                    <label class="f450">
                                        <asp:Literal runat="server" Text="<%$ Resx : Reports_ExportOnlyPrompt %>" />
                                    </label>
                                </li>
                                <li>
                                    <label class="f80">
                                        <asp:Label runat="server" Text="<%$ Resx : FileFormatColon %>" />
                                    </label>
                                    <asp:DropDownList ID="DDL_ExportFileType" runat="server">
                                        <asp:ListItem Value="0" Text="Excel" />
                                        <asp:ListItem Value="1" Text="PDF" />
                                        <asp:ListItem Value="2" Text="Word" />
                                    </asp:DropDownList>
                                </li>
                                <li class="right">
                                    <asp:Button ID="BT_CreateExport" runat="server" Text="OK" OnClick="BT_CreateExport_OnClick" />
                                    <asp:Button ID="BT_CreateReport" runat="server" Text="<%$ Resx : Reports_ContinueWithImmediateView %>" OnClick="BT_CreateReport_OnClick" />
                                    <asp:Button ID="BT_Cancel" runat="server" Text="<%$ Resx : CancelVerb %>" OnClick="BT_Cancel_OnClick" />
                                </li>
                            </ul>
                        </div>
                    </ContentTemplate>
                </asp:UpdatePanel>


    The UP_ExportPrompt updatepanel content is shown in Nyro modal window. After clicking the BT_CreateExport , excel file is created and send to the client with the Response object. I'm not pasting code for this because it's irrelevant to the problem.

    Now, I tried creating the button as trigger with

    <Triggers>
             <asp:PostBackTrigger ControlID="BT_CreateExport" />
    </Triggers>

    and it's not working again.

    Anybody knows a solution of this problem ?

    Wednesday, October 2, 2013 6:42 AM

Answers

  • User-417640953 posted

    Hi tsonev7,

    Thanks for posting to asp.net forum.

    According to your description and code you provided, I see that you want to response an attachment when click the button BT_CreateExport which in the update panel.  

    As we all know, when we do a Response.BinaryWrite, we are obliterating the standard output that the server is expecting to be able to parse through for the new markup it

    wants to send. If the button must stay in the update panel, we should add it to the panel’s trigger section as a PostBackTrigger.

    For more information.

    http://stackoverflow.com/questions/3909973/response-object-not-returning-excel-stream-in-update-panel

    I made a demo below.

    Aspx:

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <Triggers> 
                      <asp:PostBackTrigger ControlID="lbtnExportXlsKontrolni" />  
                 </Triggers> 
               <ContentTemplate> 
                <div class="divlbtn1"> 
                   <asp:LinkButton CssClass="lnkbtn" ID="lbtnExportXlsKontrolni" runat="server"  OnClick="lbtnExportXlsKontrolni_Click" >Export (Excel kontrolni)</asp:LinkButton>
                </div>
               </ContentTemplate>
    </asp:UpdatePanel>
    

    Code Behind:

    protected void lbtnExportXlsKontrolni_Click(object sender, EventArgs e)
            { 
                 
                System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath( "~/10-month/10-7/Final.xlsx"));
                ScriptManager.RegisterStartupScript(this,GetType(),"key","alert('')",true);
                if (file.Exists)
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=Final.xlsx" );
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = "application/ms-excel" ;
                    Response.WriteFile(file.FullName); 
                    Response.End();
                }
                else
                {
                 //......
                } 
    
            }
    

    If you still cannot solve it, please feedback and provide more information or code to us, then we can reproduce the scene and provide further solution. Thanks.

     

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 6, 2013 10:51 PM
  • User-1171043462 posted

    UpdatePanel does Partial PostBack i.e. AJAX call here you cannot use Response and do things like Downloading a file, etc. As downloading and uploading requires full postback

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2013 1:43 AM

All replies

  • User-417640953 posted

    Hi tsonev7,

    Thanks for posting to asp.net forum.

    According to your description and code you provided, I see that you want to response an attachment when click the button BT_CreateExport which in the update panel.  

    As we all know, when we do a Response.BinaryWrite, we are obliterating the standard output that the server is expecting to be able to parse through for the new markup it

    wants to send. If the button must stay in the update panel, we should add it to the panel’s trigger section as a PostBackTrigger.

    For more information.

    http://stackoverflow.com/questions/3909973/response-object-not-returning-excel-stream-in-update-panel

    I made a demo below.

    Aspx:

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <Triggers> 
                      <asp:PostBackTrigger ControlID="lbtnExportXlsKontrolni" />  
                 </Triggers> 
               <ContentTemplate> 
                <div class="divlbtn1"> 
                   <asp:LinkButton CssClass="lnkbtn" ID="lbtnExportXlsKontrolni" runat="server"  OnClick="lbtnExportXlsKontrolni_Click" >Export (Excel kontrolni)</asp:LinkButton>
                </div>
               </ContentTemplate>
    </asp:UpdatePanel>
    

    Code Behind:

    protected void lbtnExportXlsKontrolni_Click(object sender, EventArgs e)
            { 
                 
                System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath( "~/10-month/10-7/Final.xlsx"));
                ScriptManager.RegisterStartupScript(this,GetType(),"key","alert('')",true);
                if (file.Exists)
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=Final.xlsx" );
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = "application/ms-excel" ;
                    Response.WriteFile(file.FullName); 
                    Response.End();
                }
                else
                {
                 //......
                } 
    
            }
    

    If you still cannot solve it, please feedback and provide more information or code to us, then we can reproduce the scene and provide further solution. Thanks.

     

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 6, 2013 10:51 PM
  • Monday, October 7, 2013 12:01 AM
  • User-1171043462 posted

    UpdatePanel does Partial PostBack i.e. AJAX call here you cannot use Response and do things like Downloading a file, etc. As downloading and uploading requires full postback

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2013 1:43 AM