首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

前台JS调用后台 函数有关问题

2012-07-28 
前台JS调用后台 函数问题前台页面%@ Page LanguageC# AutoEventWireuptrue CodeBehindDefault.as

前台JS调用后台 函数问题
前台页面

<%@ 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 这种
就用上面的方法 谢谢了

[解决办法]
用Repeater 控件来做把,


可以在里面添加个 linkbutton


[解决办法]
可以把后台这段代码放到另外一个页面的Page_Load里,然后点击Input时打开新加的页面。
并且,没这样写过JS调用后台代码的。
[解决办法]
ajax+webservice
[解决办法]
362435819,+我Q,晚上QQ帮你解决

热点排行