使用Gridviews和文件传输

时间:2009-02-24 12:35:22

标签: asp.net httpwebrequest file-transfer

我需要在运行时期间在GridView中的ItemCommand_click事件上生成Excel工作表并传输文件,然后重新绑定GridView并更改状态。

当我们使用文件传输重定向响应时,如何更新GridView?

1 个答案:

答案 0 :(得分:2)

我希望在行上选定的索引更改时执行非常类似的操作,以PDF格式输出报告服务报告。我只能使用response.redirect来处理文件输出的另一个页面。看起来你的问题确实变成了状态改变后重新绑定网格,如果你做了response.redirect,你就无法触摸你的网格......

看看这段代码。我在encosia.com找到了它。看起来您可以将iFrame用于输出,然后您可以使用JavaScript调用来回发页面以重新绑定网格。

<html>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" />
<script language="javascript">
  // Get a PageRequestManager reference.
  var prm = Sys.WebForms.PageRequestManager.getInstance();

  // Hook the _initializeRequest event and add our own handler.
  prm.add_initializeRequest(InitializeRequest);

  function InitializeRequest(sender, args)
  {
    // Check to be sure this async postback is actually 
    //   requesting the file download.
    if (sender._postBackSettings.sourceElement.id == "DownloadFile")
    {
      // Create an IFRAME.
      var iframe = document.createElement("iframe");

      // Get the desired region from the dropdown.
      var region = $get("Region").value;

      // Point the IFRAME to GenerateFile, with the
      //   desired region as a querystring argument.
      iframe.src = "GenerateFile.aspx?region=" + region;

      // This makes the IFRAME invisible to the user.
      iframe.style.display = "none";

      // Add the IFRAME to the page.  This will trigger
      //   a request to GenerateFile now.
      document.body.appendChild(iframe); 
    }
  }
</script>
<asp:UpdatePanel runat="server">
  <ContentTemplate>
    <asp:DropDownList runat="server" ID="Region">
      <asp:ListItem Value="N">North Region</asp:ListItem>
      <asp:ListItem Value="W">West Region</asp:ListItem>
      <asp:ListItem Value="SE">Southeast Region</asp:ListItem>
    </asp:DropDownList>
    <asp:Button runat="server" ID="DownloadFile" Text="Generate Report" />
  </ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

处理下载的页面......

protected void Page_Load(object sender, EventArgs e)
{
  string FileResponse;
  string Region = Request.QueryString["Region"];

  // Code here to fill FileResponse with the 
  //  appropriate data based on the selected Region.

  Response.AddHeader("Content-disposition", "attachment; filename=report.csv");
  Response.ContentType = "application/octet-stream";
  Response.Write(FileResponse);
  Response.End();
}