最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I get an ASP.NET Session variable into a javascript file? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Jul 9, 2009 at 15:45 EndangeredMassa 17.5k8 gold badges56 silver badges81 bronze badges asked Jul 9, 2009 at 15:43 HcabnettekHcabnettek 12.9k38 gold badges129 silver badges192 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

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

Alternatlively, you could send the PDF file directly from the asp 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;
}

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?

发布评论

评论列表(0)

  1. 暂无评论