0

So I know that this is common problem, page freezes (any buttons visually are clickable but doesn't perform any action) because it doesn't close some request after file download.

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/txt";
response.AddHeader("Content-Disposition", "attachment; filename=" + TreeView1.SelectedNode.Text + ";");
response.TransmitFile(TreeView1.SelectedNode.Value);
response.Flush();
response.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();

This code is in TreeView SelectedNodeChanged event, and this is a problem.

For example if will put that transmit code in some button, I can fix freezing of page by adding OnClientClick="javascript:setFormSubmitToFalse()" to the button and this small JavaScript fixes page freezing after download.

<script type="text/javascript">
    //Fix page freeze after download dialog
    function setFormSubmitToFalse() {
        setTimeout(function () { _spFormOnSubmitCalled = false; }, 3000);
        return true;
    }
</script>

But I don't know how to fix this if I'm clicking on TreeView node. TreeView doesn't have OnClientClick, I also tried to run that JS function from code behind right before and after transmit code Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "setFormSubmitToFalse()", true); but it doesn't help, page is still freezing after file download.

Any ideas how can I fix this? Thanks.

0

So solution was pretty simple but a little bit tricky.

Nodes doesn't have onClientClick event, but we can add JS click event to any page element, right? So here is the trick, when creating TreeView just add click to actual text of the node with help of Span:

...
TreeNode fileNode = new TreeNode
 {
     //before
     //Text = file.Name,
     //after
     Text = "<span onclick=\"javascript:setFormSubmitToFalse();\">"+file.Name+"</span>",
     Value = file.FullName,
 };
...

And in transmitting file parse text of the node to have normal name of the file:

response.AddHeader("Content-Disposition", "attachment; filename=" + 
    TreeView1.SelectedNode.Text.Replace("<span onclick=\"javascript:setFormSubmitToFalse();\">", string.Empty).Replace("</span>", string.Empty) + ";");

Now everything is working and page is not freezing anymore after downloading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.