3

We've got a rather old asp.net webforms site that uses an asp.net linkbutton to allow a user to download a file. We originally had this as a simple a href but we had to process a button click event on the server side in order to run some back end code. Everything works fine in chrome, however, in IE (we're running IE 11) whenever you click to download a file using this linkbutton you get the following client side error:

Error: The value of the property '__doPostBack' is null or undefined, not a Function object

Our backend code does not even get triggered so we know it isn't a server side error as the debugger doesn't even hit. We did some searching to see what could be causing this and most people mentioned you need to have .net 4.5 installed on the server. But we already have .net 4.5 installed and we've run into other posts with similiar issues that simply did not apply to us.

I am trying to help another team resolve this but we are not getting anywhere with this. The link button definition is as simple as:

<asp:LinkButton runat="server" ID="lbName2" Target="_blank" OnClick="DownloadFile_Click"></asp:LinkButton>

The OnClick event is just some server side code but again we don't even hit the server side code, but if it helps here is the code:

 protected void DownloadFile_Click(object sender, EventArgs e)
        {
            var fileName = lbName2.Text;
            string newFileName;
            var serverPath = HttpContext.Current.Server.MapPath(hdnParentDocumentAbsolutePath.Value);
            //get starting point of version within file name
            //for instance mydocument(v1.0).docx
            int index = fileName.IndexOf("(v");

            if (index > 0)
            {
                //found a version number, strip it out.
                var name = fileName.Substring(0, index);
                int index2 = fileName.IndexOf(").");
                var extension = fileName.Substring(index2 + 2);
                newFileName = name + "." + extension;
            }
            else
            {
                newFileName = fileName;
            }

            //download the file
            FileInfo file = new FileInfo(serverPath + "\\" + fileName);
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + newFileName);
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
        }

When we click the linkbutton we get the script error and then we get the This page can't be displayed error in IE. It is supposed to simply download the file. Again this works fine in chrome and ff but not in IE11.

JonH
  • 32,732
  • 12
  • 87
  • 145
  • Wild stab in the dark but do you have BrowserSync enabled? – Matthew Layton Sep 06 '17 at 13:25
  • Intranet site? Is it in compatibility mode (which intranet sites often are)? – Equalsk Sep 06 '17 at 13:26
  • Could [this](https://stackoverflow.com/a/15420756/1350913) help? – IronAces Sep 06 '17 at 13:27
  • Are you running IE11 in compatibility mode? If not, what if you enable it for older version? Does it fix the problem? (in case you should insert the metatag x-ua compatible in order to force compatibility when is not enabled just for your site's page - not for all documents opened in the browser) – Sycraw Sep 06 '17 at 13:31
  • Its not a compatibility mode issue as we've tested that as well. We tried enabling compatibility mode same issue. – JonH Sep 06 '17 at 13:45

1 Answers1

5

definition of Target="_blank" causing the problem. LinkButton is not actually a link, it is a postback control. need to do definition on form.

<asp:LinkButton runat="server" ID="lbName2" OnClientClick="window.document.forms[0].target = '_blank'"  OnClick="DownloadFile_Click"></asp:LinkButton>

update:

if used in this way, the form's target attribute will remain "_blank" after the button press. There may be a problem if there are other buttons in the form.

this will be more useful...

<asp:LinkButton runat="server" ID="lbName2" data-target="_blank"  OnClick="DownloadFile_Click"></asp:LinkButton>

script for all pagess..

<script>
    $(function () {
        $('a').click(function () {
            if ($(this).data().target) {
                window.document.forms[0].target = $(this).data().target;
            } else {
                window.document.forms[0].target = '_self';
            }
        });
    });
</script>
levent
  • 3,464
  • 1
  • 12
  • 22
  • This ***ing worked!! So this whole time it was target=_blank? I should of known better, I knew a href's have this property to open in a new window. But a lot of people on other threads were saying just put _blank for the linkbutton and I saw a lot of code online where people were doing that. I guess the morale of the story is not to trust everything on the internet! – JonH Sep 06 '17 at 13:48