13

I am using an external javascript file for my asp.net project. Now i want to get the session value in that javascript. How can i get the session value in that javascript file?

Thanks in advance..

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
BINU VARGHESE
  • 364
  • 1
  • 4
  • 16

7 Answers7

21
<script>
var someSession = '<%= Session["SessionName"].ToString() %>';
alert(someSession)
</script>

This code you can write in Aspx. If you want this in some js.file, you have two ways:

  1. Make aspx file which writes complete JS code, and set source of this file as Script src
  2. Make handler, to process JS file as aspx.
hungryMind
  • 6,931
  • 4
  • 29
  • 45
6

You can access your session variable like '<%= Session["VariableName"]%>'

the text in single quotes will give session value. 1)

<script>
var session ='<%= Session["VariableName"]%>'
</script>

2) you can take a hidden field and assign value at server;

hiddenfield.value= session["xyz"].tostring();

//and in script you access the hiddenfield  like

alert(document.getElementbyId("hiddenfield").value);
evictednoise
  • 587
  • 1
  • 7
  • 19
  • 1
    You have an error, it should be `<%= Session["Variable"] %>`, the square bracket not parenthesis. – Greg Jul 14 '15 at 14:46
1

I tried following with ASP.NET MVC 5, its works for me

var sessionData = "@Session["SessionName"]";
Kelum
  • 1,745
  • 5
  • 24
  • 45
1

For me this code worked in JavaScript like a charm!

<%= session.getAttribute("variableName")%>

hope it helps...

0
protected void Page_Load(object sender, EventArgs e)
    {
        Session["MyTest"] = "abcd";

        String csname = "OnSubmitScript";
        Type cstype = this.GetType();

        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;

        // Check to see if the OnSubmit statement is already registered.
        if (!cs.IsOnSubmitStatementRegistered(cstype, csname))
        {
            string cstext = " document.getElementById(\"TextBox1\").value = getMyvalSession()  ; ";
            cs.RegisterOnSubmitStatement(cstype, csname, cstext);
        }

        if (TextBox1.Text.Equals("")) { }
        else {
              Session["MyTest"] = TextBox1.Text;
        }

    }


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <script language=javascript type="text/javascript">
        function getMyvalSession() {

             var txt = "efgh";
             var ff = '<%=Session["MyTest"] %>' + txt;
             return ff ;
        }
    </script>

</head>
<body>

    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"  AutoPostBack=true ></asp:TextBox>
     <input type="submit"  value="Submit" />
    </div>
    </form>
</body>
</html>
0
var sessionVal = '@Session["EnergyUnit"]';
alert(sessionVal);
Ahmad
  • 12,336
  • 6
  • 48
  • 88
0

If you are using VB as code behind, you have to use bracket "()" instead of square bracket "[]".

Example for VB:

<script type="text/javascript">
var accesslevel = '<%= Session("accesslevel").ToString().ToLower() %>';
</script>  
LifeiSHot
  • 141
  • 2
  • 4