-1

am having Sample.aspx and sample.js both are in different directory.

how can i get the Sample.aspx page session values in sample.js file ?

i cant get the value for the following types

    function session() {
        alert('<%=Session.getAttribute("hdn_CheckedData")%>');
        alert(document.getElementbyId("hdn_CheckedData").value);
        alert('<%=Session["CheckedData"]%>');
        alert('<%=Session("CheckedData")%>');
        alert('<%=Session["CheckedData"].ToString()%>');
        alert('<%=Session("CheckedData").ToString()%>');
    };

CheckedData - is the session

hdn_CheckedData - is the hiddenfield

i tried both of it.

is it possible then help me pls.....

Hiddenfiled, session, viewstate or anything............

Naveen Desosha
  • 347
  • 5
  • 12

4 Answers4

2

One simple solution is to declare the session variables just before the load of your javascript file and inside the aspx page. Eg on sample.aspx you have

<script>
var sessionCheckData = "<%=session["CheckedData"].ToString()%>";
</script>
<script type="text/javascript" src="sample.js"></script>

and on sample.js you have

  function session() {
        alert(sessionCheckData);
    };

Similar answer: How to get asp.net client id at external javascript file

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • I don't think backticks are valid wrapper for JavaScript strings. [confirmed](http://jsfiddle.net/Vz47j/). – Shadow The GPT Wizard Dec 19 '12 at 13:41
  • @ShadowWizard I Lost you. What I am try to say here is the inside the javascript can not render the session variable, so can he render it before. – Aristos Dec 19 '12 at 13:43
  • While @ShadowWizard's solution will work, it makes the script dependent on a "global" variable that session.js does not if it will provided. I would recommend that rather than set the variable, why not simply pass it in as a parameter to the session() method? – Andy T Dec 19 '12 at 17:52
  • @QuetiMporta you miss the point; JavaScript can't directly read session variables and you can't have `<%=` in .js file since it's not parsed by the server. – Shadow The GPT Wizard Dec 19 '12 at 19:17
  • You are correct, however, you can still pass in the session variable by embedding it as the parameter to session()... – Andy T Dec 19 '12 at 19:20
0

You can save the session data to a hidden field on your page. After that you have direct access to that field in you js file.

Or you can declare a session variable as Aristos has proposed. That would more straightforward actually.

CoffeeCode
  • 4,296
  • 9
  • 40
  • 55
0

I would write a generic handler and use JSONP to pass any data to external Javascript.

Please take a look at here, here and here.

Since it is not cross domain, JSON should also work.

Community
  • 1
  • 1
Ertug
  • 301
  • 1
  • 5
0

You have to assign session values to hiddenfield

after that you can use that values

Aarif Qureshi
  • 474
  • 1
  • 3
  • 13