-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............

4 Answers 4

1

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

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

5 Comments

I don't think backticks are valid wrapper for JavaScript strings. confirmed.
@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.
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?
@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.
You are correct, however, you can still pass in the session variable by embedding it as the parameter to session()... <script type="text/javascript" src="sample.js"></script><script> session("<%=session["CheckedData"].ToString()%>"); </script>
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.

Comments

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.

Comments

0

You have to assign session values to hiddenfield

after that you can use that values

Comments

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.