1

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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?

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.