Inloggen
 
 
 
 
    
Pushing a file which is generated runtime in an ASP.NET 2.0 AJAX-enabled environment
Location: BlogsFerry Onderwater - Developer    
Posted by: Ferry Onderwater 20-3-2008 22:21
We have a page on which we can enter some values. When pushing on a button a file is generated using the entered values. While generation the file, a progress-indicator is shown. After the file has been generated we want to show it in a new window.

Unfortunately we can't use Response.Redirect because it is not possible to open a new browser windows server side. Response.Write isn't possible since using this will trash Ajax (Sys.WebForms.PageRequestManagerParserErrorException). Therefore this has to be done client side.

We will make use of the Ajax endRequest event and combine that with the dataItem functionality.

On the aspx-page we add the following script. We add it below the update-panel endtag.

Code:

<SCRIPT language=javascript type=text/javascript>
Sys.WebForms.PageRequestManager.getInstance().add_endRequest( function EndRequestHandler(sender, args)
{
if (args.get_error() == undefined)
{
var dataItems = args.get_dataItems();
if ($get('hlDownload') !== null)
{
window.open(dataItems['hlDownload']);
}
}
}
)
</SCRIPT>

This eventhandler runs after each partial postback. First we check if an error occurred. If this isn't the case we retreive the dataItems. This items can be set from server side and must be 'bound' to a control. That means the data will be received by the given control.
Server side we set a dataItem after the file has been generated. Since we want to give the user a link to download the file when for example the new window was blocked we put a hyperlink on the page. This control is invisible until the file is available. Invisible means it isn't rendered and therefor can't be found by client code.
After the file has been generated we make the control visible and are therefore able to use it to receive a dataItem.
This is done server side by using the following code:

Code:

ScriptManager1.RegisterDataItem(hlDownload, _filename)

We have created a dataItem which contains the URL of the generated file and send that Item to the hyperlink.

Now the endRequest eventhandler is able to 'get' the hyperlink and retreive the dataItem send to that link. So all information we need is available, client side! Simply using a window.open completes our goal.

Copyright ©2008 Ferry Onderwater
Permalink |  Trackback
  
 
Weblogs
    
Archief
    
Zoeken
    
 
 
 
 
Copyright 2006-2009 by Arcencus
Privacy Statement | Terms Of Use