views:

1680

answers:

3

I am refactoring a legacy web app. This app has a is using the onload event inside the body tag (On the Master page) to run this javascript script. Note this script tag is after the form element in the doc. I know the syntax looks hideous (or Visual Studio at least tells it is by the squiggles), but I'll be darned, the thing DOES indeed work.

function DisplayPDF()
{
    var strPDF
    strPDF = "<%=SESSION("PDF")%>";
    if (strPDF.length != 0)
     {
        window.open(strPDF);
        <%Session("PDF") = ""%>
     }
}

My question is I'm trying to develop a more elegant solution. I have ASP.NET ajax and jQuery both available to me. I wrote a tiny asp.net ajax component that I want to use to handle this.

Type.registerNamespace("ck");

ck.pdfOpener = function() {
  ck.pdfOpener.initializeBase(this);
}

ck.pdfOpener.prototype = {
    initialize: function() {
        ck.pdfOpener.callBaseMethod(this, 'initialize');
    },

    dispose: function() {        
        ck.pdfOpener.callBaseMethod(this, 'dispose');
    },

    openPDF: function(){
       //HOW CAN I RETRIEVE A SESSION VARIABLE HERE???
    }
}

ck.ClientControl.registerClass('ck.pdfOpener', Sys.Component);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

Can/Should I be doing it this way? Or should I create a WebService that returns said variable. Thanks for any advice.

Cheers,
~ck in San Diego

+1  A: 

Use the Page.ClientScript.RegisterClientScriptBlock(typeof(YOURPAGECLASS), "KEY", "ACTUALJSCODE"); in your code behind (i.e. the Page_Load event handler)

Colin
A: 

A simple way to do this would be as you said simply make an Ajax call to a page method, MVC action, HttpHandler or web service that returns the required value from the session. The bigger question here is why you are storing the path to a PDF file in your session state?

KnackeredCoder
In this app, on a page, say "createInvoice.aspx", there is a button that runs an active report based on screen content, that report is saved as a PDF, this is the path to that newly created PDF. The requirement here is to popup the newly created PDF. Clicking the button posts the form back and the onload handler sees there is a value so it opens a new window using that file path. I have begged to redesign the is but management wants to go with it as it works. They are, of course, very "show me" oriented, so they always want to see something new. No time to clean thing up. :( Any ideas?
Hcabnettek
A: 

Alternatlively, you could send the PDF file directly from the asp.net page instead of within the client script.

string pdfFile = Session("PDF");
if (!string.IsNullOrEmpty(pdfFile))
{
  Session.Remove("PDF");

  Response.ContentType = "application/pdf";
  FileInfo fi = new FileInfo(pdfFile);
  Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Path.GetFileName(pdfFile) + "\"");
  Response.AddHeader("Content-Length", fi.Length.ToString()); 
  Response.WriteFile(pdfFile);
  Response.Flush();
  return;
}
cdm9002
This works beautiful! Thanks for the advice!
Hcabnettek