views:

70

answers:

1

I need to generate a Excel sheet during RunTime on a ItemCommand_click event in a GridView and transfer the file and then re-bind the GridView with the status change.

As we redirect the response with File transfer , How could I update the GridView?

+2  A: 

I was looking to do something very similar on selected index change on the rows to output a reporting services report as a PDF. I was only able to get this to work with a response.redirect to another page that handled the output of the file. It looks like your issue really becomes the rebinding the grid after the status change, if you do a response.redirect, you can't touch your grid...

Take a look at this code. I found it on encosia.com. It looks like you might be able to use the iFrame for your output, and then you can use a JavaScript call to postback the page to rebind the grid maybe.

<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>

The page that handles the download...

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();
}
RSolberg
logic lloks good...Will try this oneBTW I think rather creating a .aspx for file handling, we can go for .ashx (HTTP Handler); wat say??
novice
I'm not sure. Haven't worked in that area too much, but it may work for you.
RSolberg