locked
Get session variable in javascript RRS feed

  • Question

  • User1673853347 posted

    I want to access my session array in javascript

    but when i m trying to get it using following code in externaly embeded javascript

    var mySessionVar="<%=Session("logindept").ToString();%>";

    i got the error as 

    ReferenceError: Session is not defined

    why this is happenimg?

    and what can i do if i want to access these values............. pls help me I am new in ASP.net

    Tuesday, December 9, 2008 6:07 AM

Answers

  • User365878951 posted

     Session is a Server Side object it cann't accessed directly in the Javascript as it calls in the Client Side.

     So, place your Session value in a hidden filed or pass the value in the Session as a parameter while calling the Script.

    javascript:return funtion(Session["logindept"].ToString())

    this might work for you.

    else take a hidden field

    <input type="hidden" runat="server" name="id"/>  --- in aspx

    id.value=Session["logindept"].ToString(); ---- in  CS

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 9, 2008 7:53 AM
  • User-768391503 posted

     I would avoid using <%= %> in my html markup.

    Look at using RegisterStartUpScript to output the variable to the page with the Session.

    Eric

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 9, 2008 8:04 AM
  • User1839833660 posted

     Try this code

    <script language="JavaScript">
    var mySessionVar="<%= Session["SessionVar"] %>";
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 10, 2008 3:47 AM
  • User-2090943682 posted

    string popval;
    popval = Session["SessionID"];


    AddJavascriptCode(popval)


    private void AddJavascriptCode(int popval)
        {
            string script = @"<script language=""JavaScript"" type=""text/javascript"">
           
        
            alert(popval);
       
            </script>";

          
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Create", script);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 10, 2008 4:06 AM
  • User2115139740 posted

    in javascript tag add below line then you can sue session value in javascript

    var UserID='<%=Session("UserID")%>';

     

    then perform you operation depend on UserID.

    I am using like this way and working fine.

    hope this will help you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 10, 2008 4:13 AM
  • User798903548 posted

    1. You can't because the <%= %> construct does not work in include files.
    2. Why would you make 2 posts on the same problem? Answered here: http://forums.asp.net/t/1104368.aspx?PageIndex=2

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 10, 2008 9:12 AM

All replies

  • User365878951 posted

     Session is a Server Side object it cann't accessed directly in the Javascript as it calls in the Client Side.

     So, place your Session value in a hidden filed or pass the value in the Session as a parameter while calling the Script.

    javascript:return funtion(Session["logindept"].ToString())

    this might work for you.

    else take a hidden field

    <input type="hidden" runat="server" name="id"/>  --- in aspx

    id.value=Session["logindept"].ToString(); ---- in  CS

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 9, 2008 7:53 AM
  • User-768391503 posted

     I would avoid using <%= %> in my html markup.

    Look at using RegisterStartUpScript to output the variable to the page with the Session.

    Eric

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 9, 2008 8:04 AM
  • User1673853347 posted

    I am calling javascript  as u said by passing session like bellow

    <form id="form1" runat="server" onclick="GetDeptNames('Session["logindept"].ToString()')" >

    but this is syntactically not correct..... i got error as

     missing ) after formal parameters

     function GetDeptNames(Session["logindept"].ToString())  and then i tried for diffrent diffrent combinations of single and double quotes but all failed

    i would like to clear one point that my session is array of string and not single string

    then i tried by using hidden filed i assigned value to hidden field as in one form which is master form
     
    Hidden1.Value = Session["logindept"].ToString();

     and in External javascripts file i have function in which i tried to get that value as

    alert(document.getElementById('Hidden1').value);

    but there also i got the error

    document.getElementById("Hidden1") has no properties
     
    where i am doing wrong? pls help me this is really urgent to me.....
    what i do?pls tell me in details......
    Wednesday, December 10, 2008 3:29 AM
  • User1673853347 posted

    Will u pls tell me little bit in details how i can use this RegisterStartUpScript .......

    bz as i posted above i am getting syntax error........... :'(

    Wednesday, December 10, 2008 3:42 AM
  • User1839833660 posted

     Try this code

    <script language="JavaScript">
    var mySessionVar="<%= Session["SessionVar"] %>";
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 10, 2008 3:47 AM
  • User-2090943682 posted

    string popval;
    popval = Session["SessionID"];


    AddJavascriptCode(popval)


    private void AddJavascriptCode(int popval)
        {
            string script = @"<script language=""JavaScript"" type=""text/javascript"">
           
        
            alert(popval);
       
            </script>";

          
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Create", script);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 10, 2008 4:06 AM
  • User1673853347 posted

     as u said i write the code ..........

    function GetDeptNames()

    {

    debugger;

    var mySessionVar="<%=Session["logindept"]%>";

    alert(mySessionVar);

     

    }

     but there is error as ...........

     missing ; before statement

     So whats wrong....

    Wednesday, December 10, 2008 4:11 AM
  • User2115139740 posted

    in javascript tag add below line then you can sue session value in javascript

    var UserID='<%=Session("UserID")%>';

     

    then perform you operation depend on UserID.

    I am using like this way and working fine.

    hope this will help you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 10, 2008 4:13 AM
  • User1673853347 posted

    Hi

    When i did the way u said ..... it not gave me any error but it showed me session name as string

    <%=Session("logindept")%>

    Actully i want the values which i passed through this session ...and the session["logindept"] contains array of string so how can i access these values in javascript?????

    and one more thing my javascript file is external .

    Wednesday, December 10, 2008 4:27 AM
  • User1673853347 posted

    when i didi like this

    string popval;
    popval = Session["SessionID"];


    AddJavascriptCode(popval)


    private void AddJavascriptCode(int popval)
        {
            string script = @"<script language=""JavaScript"" type=""text/javascript"">
           
        
            alert(popval);
       
            </script>";

          
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Create", script);
        }

     

    i got the error as ..........    popval is not defined and i this this is because client side script cant access the severside values

    Wednesday, December 10, 2008 4:41 AM
  • User1673853347 posted

    heloooo manoj

    i am sorry bz u r solution is right but its not working with external javascript......

    i think this solution is fine with the internal javascript

    but still there is problem if we try to retrive the string array using session

    can u help for this?

     

    Wednesday, December 10, 2008 6:28 AM
  • User798903548 posted

    1. You can't because the <%= %> construct does not work in include files.
    2. Why would you make 2 posts on the same problem? Answered here: http://forums.asp.net/t/1104368.aspx?PageIndex=2

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 10, 2008 9:12 AM