locked
Updatepanel and response.binarywrite issue RRS feed

  • Question

  • User239026615 posted

    I have an UpdatePanel that contains a button. There is also an UpdateProgress with a label. When the button is clicked the label shows text that tells the user what is going to happen.  Then I run a routine that packages a number of binary data files into a zip file that can then be downloaded using response.binarywrite.  I have discovered that I cannot use response.binarywrite with an update panel unless I add a postback trigger to the button. However, when I do this the label message no longer appears. So if the label displays, the response.binarywrite does not work and if the response.binarywrite works, the label does not display. How can I get both?

    Thursday, August 14, 2014 3:05 PM

Answers

  • User-760709272 posted
    <form id="form1" runat="server">
        
    <asp:ScriptManager runat="server" />
    
    <asp:UpdatePanel runat="server">
    <ContentTemplate>
    <asp:Literal ID="LiteralMessage" runat="server" />
    <asp:Button ID="ButtonSubmit" runat="server" Text="Submit" OnClick="ButtonSubmit_Click" />
    <asp:Literal ID="LiteralIFrame" runat="server" />
    </ContentTemplate>
    </asp:UpdatePanel>
     
    
    </form>
    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        LiteralMessage.Text = "<p>Your file is downloading</p>";
    
        // the url to a page that will stream the file to the client
        string url = "/downloadfile.aspx?filename=yourfile.zip";
        LiteralIFrame.Text = string.Format("<iframe style=\"width:0px;height:0px;display:none;\" src=\"{0}\"></iframe>", url);
    
        ButtonSubmit.Visible = false;
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 14, 2014 6:22 PM

All replies

  • User-760709272 posted

    When you trigger the updatepanel to update the label, you can also use that post back to show an iframe that has the url of a page that does your response.binarywrite on it.  Make the iframe 1px by 1px and maybe off screen so that it can't be seen, but it will cause the browser to request the page with the binary write which will then prompt a download dialog.

    Thursday, August 14, 2014 5:14 PM
  • User239026615 posted

    I have not used iframe and I am not lazy, but after a day and a half of trying to make this work a brief example would be greatly appreciated.

    Thursday, August 14, 2014 5:59 PM
  • User-760709272 posted
    <form id="form1" runat="server">
        
    <asp:ScriptManager runat="server" />
    
    <asp:UpdatePanel runat="server">
    <ContentTemplate>
    <asp:Literal ID="LiteralMessage" runat="server" />
    <asp:Button ID="ButtonSubmit" runat="server" Text="Submit" OnClick="ButtonSubmit_Click" />
    <asp:Literal ID="LiteralIFrame" runat="server" />
    </ContentTemplate>
    </asp:UpdatePanel>
     
    
    </form>
    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        LiteralMessage.Text = "<p>Your file is downloading</p>";
    
        // the url to a page that will stream the file to the client
        string url = "/downloadfile.aspx?filename=yourfile.zip";
        LiteralIFrame.Text = string.Format("<iframe style=\"width:0px;height:0px;display:none;\" src=\"{0}\"></iframe>", url);
    
        ButtonSubmit.Visible = false;
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 14, 2014 6:22 PM
  • User239026615 posted

    Much appreciated.  I will try it tomorrow and mark as answer if successful.  Thanks again.

    Thursday, August 14, 2014 6:28 PM
  • User239026615 posted

    It is working, but how do I make the LiteralMessage go away when the process is finished?

    Friday, August 15, 2014 10:33 AM
  • User-760709272 posted

    You can't really do that as your code doesn't know when, or if, the download has finished.

    Friday, August 15, 2014 2:33 PM