Form Control postback

James Mosquito asked on October 21, 2020 12:36

Hi I am creating a form control and it contains a button that when clicked will create and trigger a file to download. The file download is working fine but on postback the default Kentico loading modal appears and remains - forcing you to navigate away from the page and back to it. How should I handle this?

The code I am using is included below and has been shortened for brevity:

MyControl.ascx

<cms:CMSUpdatePanel ID="Panel" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:PostBackTrigger ControlID="btnDownload" />
    </Triggers>
    <ContentTemplate>
        <asp:Button ID="btnDownload" runat="server" Text="Download" CssClass="btn btn-default" />
    </ContentTemplate>
</cms:CMSUpdatePanel>

MyControl.ascx.cs

public void Download_Click(object sender, EventArgs e)
{
    var exampleFilePath = Server.MapPath("~/example.csv");

    using (var writer = new StreamWriter(exampleFilePath))
    {
        writer.WriteLine("\"Column1*\",\"Column2*\",\"Column3\",\"Column4*\",\"Column5*\"");
    }

    Response.ContentType = "text/csv";
    Response.AppendHeader("Content-Disposition", "attachment; filename=example.csv");
    Response.TransmitFile(exampleFilePath);
    Response.End();
}

Thanks, James

Recent Answers


James Mosquito answered on October 22, 2020 09:27

I managed to resolve this by closing the loader using a timer in the onclick event of the button:

Button:

<asp:Button ID="btnDownload" runat="server" Text="Download" OnClientClick="setTimeout(closeLoader, 1000);" CssClass="btn btn-default" />

JS:

<script type="text/javascript">
    function closeLoader() {
        if (window.Loader) {
            window.Loader.hide();
        }
    }
</script>

It feels a little hacky so I'm open to alternative suggestions.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.