views:

2952

answers:

5

Here's my situation.

I have a button on my ASP.NET webform. This button creates a new browser window pointing to a page which has a lot of hidden fields (which are dynamically generated). This form submits itself to SQL Reporting Services on the bodies onload event. This works fine and the report is displayed in this new window.

However, now I want to still POST a form to SQL Reporting services but I want to get back an excel spreadsheet. So I add another hidden input with a name of rs:Format and value of Excel. This works and the user gets the option to download the excel file.

However they are now stuck with the extra window that was created. How do I get around this? I've tried creating the dynamic form and POST in the same window, but then they see the (empty) page with the form, and not the page they generated the report from. I've tried closing the window that I've created but I don't know where to put the javascript to do this. If I put it on the onload, then the window closes without the form being submitted.

Any ideas for what to do here?

Edit: What I was doing here wasn't the best way of getting the result I needed. I ended up using a WebRequest to get the excel report from Reporting Services instead posting a form, therefore I didn't need the second window afterall.

+3  A: 

Don't close the browser. It belongs to the user, even if you opened it. Closing it can make them mad.
Do redirect to a page the communicates to the user that you're done with the window. There you can provide a (javascript-based) link that make closing the browser a little easier if you want, though closing a browser window is generally pretty easy.

Joel Coehoorn
The window was only generated to post a form it generated to SSRS. Ideally I wouldn't need this window at all, but the user has no interaction with it.
Ray
Still don't close the window. Doing so can cause annoying issues: if the user sees the window pop up and instinctually goes to close it, but clicks after your window.close() call takes place, their click passes through to whatever is below their cursor in the next active window.
eyelidlessness
+1  A: 

When user wants an Excel file, there's no need to pop up another window. I assume selection of Excel file or HTML report is done in some HTML control like a radio button or a checkbox. So, before doing anything, check the value of that radiobutton/checkbox with javascript and do the appropriate action. Something like:

function getReport(excelFormat)
{
    if (excelFormat)
        document.form1.target = '_blank';
    else
        document.form1.target = '_self';
    document.form1.submit();
}
Milan Babuškov
Yes, this is really what I want. However I need server side code to put together all the parameters I need to submit to SSRS, and that way I'm doing this currently is to create a form to post to SSRS. I think this is where I'm going wrong in my approach.
Ray
A: 

Generally it's ok to close any popup window that your app has created.

This can be done with window.close() (which will pop up a confirmation if the window was not created by script).

If you want to be sure that the download is successful before closing the window, you will need to perform some server-side magic - have your server keep track of the download in progress, and poll it via AJAX from the popup window until the download completes.

Once the server tells you it's done, the window can be closed.

levik
+2  A: 

By the way, if the popup doesn't contain any useful output, what you may want to do is submit your form into a small Iframe within the page. This way there's no need to close a window, as the frame can be made invisible.

levik
+1  A: 

What if the button did an Ajax request back to the original page and got the hidden field values. You could then construct another form on the page with the hidden fields using javascript and submit it -- with the download option. Since the request will return an application/ms-excel file, it shouldn't refresh the current page but the download should still occur. You'd need to make sure that the button click didn't cause a postback by returning false from the client-side function. Note that this only works if the post of the generated form results in a download, not a new html page.

<script type="text/javascript">
   function submitReport( button ) {
      PageMethod.SubmitReport(onSuccess,onFailure,{ control: button });
   }

   function onSuccess(values,ctx) {
      var form = document.createElement('form');
      form.action = reporting-services.url;
      form.method = 'post';
      document.body.appendChild(form);
      .... add hidden fields to form from returned values
      form.submit();
      document.body.removeChild(form);
   }

   function onFailure(error,ctx) {
      ... pop up some error message....
   }

  </script>

  ...

  <asp:Button runat="server" id="reportButton" ClientClick="submitReport(this);return false;" Text="Report" />
tvanfosson