I am seeing some strange interaction between my javascript and code behind page. Here is the page:
<form name="form1" id="form1" runat="server">
<button onclick="submitForm()">Submit Form</button>
<asp:LinkButton runat="server" OnClick="btn_download">Download!</asp:LinkButton>
</form>
<script type="text/javascript">
function submitForm()
{
document.form1.submit();
}
</script>
and the code behind:
protected void btn_download(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", String.Concat("attachment; filename=", "download.txt"));
string hw = "hello world!";
byte[] info = new byte[hw.Length * sizeof(char)];
System.Buffer.BlockCopy(hw.ToCharArray(), 0, info, 0, info.Length);
Response.OutputStream.Write(info, 0, info.Length);
Response.Flush();
Response.End();
}
When I hit the download button I get a file as expected. After hitting download, if I hit the submit form button the form gets submitted but also it acts as though the download was pressed - triggering the btn_download method. If I comment out the code in btn_download(..) this does not occur.
If I hit the submit form button before hitting the download button it does not trigger btn_download. I tried commenting out the code behind line by line and it appears that the line Response.appendHeader(..) is causing the issue. Can someone explain what is going on here? Why does form1.submit() act as though I clicked download?