前台JS调用后台 函数问题

luckface 2012-03-31 05:10:40
前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DownLoadFile._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="submit" value="提交" id="OK" />
<input visible="false" id="Hidden" />
</div>
</form>
<script>
$("#id").click()
{
document.write("<%ExportToExcel(); %>");
}
</script>
</body>
</html>

后台代码

public void ExportToExcel()
{
string strFile = " ";
string path = " ";
strFile = strFile + "export_ ";
strFile = strFile + DateTime.Now.ToString("yyyyMMddHHmm ");
strFile = strFile + ".xls ";
string fileName = strFile;
path = Server.MapPath("/") + "DownLoad\\" + fileName;
System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());

/*DataRow[] myRow = dt.Select("");

//取得数据表各列标题,标题之间以\t分割,最后一个列标题后加回车符
for (int i = 0; i < dt.Columns.Count; i++)
{
sw.Write(dt.Columns[i].Caption.ToString() + "\t");
}
sw.Write("\n");
//逐行处理数据
foreach (DataRow row in myRow)
{
//在当前行中,逐列取得数据,数据之间以\t分割,结束时加回车符\n
for (int i = 0; i < dt.Columns.Count; i++)
{
sw.Write(row[i].ToString() + "\t");
}
sw.Write("\n");
}*/

sw.Flush();
sw.Close();
if (path != null)
{
//DownFile(path,fileName);
try
{
DownFile(path, fileName);
//DownFile(path, fileName);
}
catch (Exception e)
{
Response.Write("<script>alert('系统出现以下错误:\\n" + e.Message + "!\\n请尽快与管理员联系.')</script>");
}
}
}

private void DownFile(string path, string fileName)
{
//string fileName = "asd.txt";//客户端保存的文件名

//string filePath = Server.MapPath(path);//路径

FileInfo fileInfo = new FileInfo(path);

Response.Clear();

Response.ClearContent();

Response.ClearHeaders();

Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);

Response.AddHeader("Content-Length", fileInfo.Length.ToString());

Response.AddHeader("Content-Transfer-Encoding", "binary");

Response.ContentType = "application/octet-stream";

Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

Response.WriteFile(fileInfo.FullName);

Response.Flush();

Response.End();
}


问题是 : 页面一加载就弹出下载提示!我想在点击按钮后再执行下载,
不用<asp:button 这种
就用上面的方法 谢谢了
...全文
627 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
w362435819 2012-07-05
  • 打赏
  • 举报
回复
362435819,+我Q,晚上QQ帮你解决
sihuashanxq 2012-07-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
ajax+webservice
[/Quote]
+1
晓莜 2012-07-05
  • 打赏
  • 举报
回复
同上 ajax
色拉油 2012-03-31
  • 打赏
  • 举报
回复
ajax+webservice
gavinwwl 2012-03-31
  • 打赏
  • 举报
回复
可以把后台这段代码放到另外一个页面的Page_Load里,然后点击Input时打开新加的页面。
并且,没这样写过JS调用后台代码的。
luckface 2012-03-31
  • 打赏
  • 举报
回复
我现在 只能用我上面那种方法!
Im_Sorry 2012-03-31
  • 打赏
  • 举报
回复
用Repeater 控件来做把,


可以在里面添加个 linkbutton

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧