locked
Get session variable in javascript RRS feed

  • Question

  • User-376222682 posted
    HI all, There is any possible way to get session variable in javascript ?? I used asp.net 1.1 and vb.net Thanks Sajjad
    Friday, May 4, 2007 2:33 AM

Answers

  • User-154051931 posted

    See my reply here:

    http://forums.asp.net/thread/1685859.aspx

    Hope this helps!  Don't forget to mark the most helpful post(s) as Answer for the sake of future readers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 4, 2007 2:58 AM
  • User-144522783 posted

    <asp:HyperLink Runat="server" Font-Name="Arial" Font-Size="16px" ForeColor="black" ID="Hyperlink_to_vacancy" Target="_self" Text='<%# DataBinder.Eval(Container.DataItem, "JobDesignation") %>' NavigateUrl="javascript:Openview('<%# DataBinder.Eval(Container.DataItem, "vacancy_id") %>')">
                  </asp:HyperLink>
     

     

    Hi,

    try

    <asp:HyperLinkField HeaderText="Type Name" SortExpression="Name" DataTextField="JobDesignation" DataNavigateUrlFormatString="javascript:Openview({0})" DataNavigateUrlFields="vacancy_id">

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 1, 2008 3:55 AM

All replies

  • User541108374 posted

    Hi,

    normally I would say no because javascript is on the client and Sessions are on the server. However a scenario that you could give a try would involve AJAX. Create a webservice that's able to read the session object you're interested in and you could call that when appropriate from your client javascript code. If you're using ASP.NET AJAX you could also use PageMethods, remember to set the EnablePageMethods property on the ScriptManager control, so you don't need a dedicated webservice.

    Update: I guess I'm just too busy learning AJAX at the moment that I want to solve everything with it [8-|]. The suggestions provided by the other members are also applicable to your problem. If you however want to decide at runtime in javascript which session to use you could use AJAX.

    Grz, Kris.

    Friday, May 4, 2007 2:37 AM
  • User-1386562875 posted

    You could write some javascript on the server-side and put the session variable in it and the render the result to your page.

     

    Friday, May 4, 2007 2:40 AM
  • User-376222682 posted
    Thanx for reply... JohanNL can u send me the sample code... bcz i have no idea about that Thanks once again Sajjad
    Friday, May 4, 2007 2:49 AM
  • User-154051931 posted

    See my reply here:

    http://forums.asp.net/thread/1685859.aspx

    Hope this helps!  Don't forget to mark the most helpful post(s) as Answer for the sake of future readers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 4, 2007 2:58 AM
  • User1001737435 posted

    you can access your session variable like '<%= Session("VariableName")%>'

     the text in single quotes will give session value.

    regards,
     

    Friday, May 4, 2007 2:59 AM
  • User-376222682 posted
    Thanks Josh :)
    Your's code is working..........
    Thanks to All

    Sajjad
    Friday, May 4, 2007 3:48 AM
  • User-1068907443 posted

    Thanks 4 the Help...but i got this error

    A name was started with an invalid character.

               string a ='<%=Session["MemberType"]%>';
    -----------------------^

    Thursday, January 31, 2008 2:19 AM
  • User-144522783 posted

    hi,

    r u putting this code in external js?  

    Thursday, January 31, 2008 2:32 AM
  • User-1068907443 posted

    thanks 4 the response

    i was not putting it in external js there was some other problem in my script,

    a ="<%=Session("MemberType")%>";  is working fine.

    actually i wanna to pass datagrid item value as parameter to javascript

    <%# DataBinder.Eval(Container.DataItem, "CompanyName") %>'<%# DataBinder.Eval(Container.DataItem, "CompanyName")%>'<%# DataBinder.Eval(Container.DataItem, "CompanyName") %>

    <%# DataBinder.Eval(Container.DataItem, "CompanyName") %>'<%# DataBinder.Eval(Container.DataItem, "CompanyName") %> but ..... not able to get the solution

     

    Thursday, January 31, 2008 8:39 AM
  • User-144522783 posted

    hi,

    post your code 

    Thursday, January 31, 2008 10:34 PM
  • User-1068907443 posted

    thanx..alot 4 the response 

    now i have done it but  in code behiend ........... actually i wanna to send user according to his/her menbertype through javascript nd two parameters are required one is action nd second one is vacancy id......i m fetching them using Request.QueryString["vacancy_id"];

     my javascript

    function Trim(str)
    {  while(str.charAt(0) == (" ") )
      {  str = str.substring(1);
      }
      while(str.charAt(str.length-1) == " " )
      {  str = str.substring(0,str.length-1);
      }
      return str;
    }

    function Openview(vid)
    {
      alert(vid);
           var a;
               a ='<%=Session["MemberType"]%>';
         
           var s=Trim(a);
           if(s.toUpperCase()=="STUDENT")
           {
         location.href='view_vacancy.aspx?action=view'+'&vacancy_id'+'='+ vid;
             return false;
           }
           else
           {
            
            location.href='form.aspx?action=view'+'&vacancy_id'+'='+ vid;
             return false;
           }
      
    }

    </script>

     

    my ascx page

    here is the call to javascript......

    i m using datagrid...

    <asp:HyperLink Runat="server" Font-Name="Arial" Font-Size="16px" ForeColor="black" ID="Hyperlink_to_vacancy" Target="_self" Text='<%# DataBinder.Eval(Container.DataItem, "JobDesignation") %>' NavigateUrl="javascript:Openview(this.DataKeyField)">
                  </asp:HyperLink>

    in this case error........vacancy id is going undefined in code behind.....

    or

    <asp:HyperLink Runat="server" Font-Name="Arial" Font-Size="16px" ForeColor="black" ID="Hyperlink_to_vacancy" Target="_self" Text='<%# DataBinder.Eval(Container.DataItem, "JobDesignation") %>' NavigateUrl="javascript:Openview('<%# DataBinder.Eval(Container.DataItem, "vacancy_id") %>')">
                  </asp:HyperLink>

     

    error ......error in page .... no display

    <script type=text/javascript> function Trim(str) { while(str.charAt(0) == (" ") ) { str = str.substring(1); } while(str.charAt(str.length-1) == " " ) { str = str.substring(0,str.length-1); } return str; } function Openview(vid) { alert(vid); var a; a ='<%=Session["MemberType"]%>'; var s=Trim(a); if(s.toUpperCase()=="STUDENT") { location.href='view_vacancy.aspx?action=view'+'&vacancy_id'+'='+ vid; return false; } else { location.href='form.aspx?action=view'+'&vacancy_id'+'='+ vid; return false; } } </script> <script type=text/javascript> function Trim(str) { while(str.charAt(0) == (" ") ) { str = str.substring(1); } while(str.charAt(str.length-1) == " " ) { str = str.substring(0,str.length-1); } return str; } function Openview(vid) { alert(vid); var a; a ='<%=Session["MemberType"]%>'; var s=Trim(a); if(s.toUpperCase()=="STUDENT") { location.href='view_vacancy.aspx?action=view'+'&vacancy_id'+'='+ vid; return false; } else { location.href='form.aspx?action=view'+'&vacancy_id'+'='+ vid; return false; } } </script>
    Thursday, January 31, 2008 11:30 PM
  • User-144522783 posted

    <asp:HyperLink Runat="server" Font-Name="Arial" Font-Size="16px" ForeColor="black" ID="Hyperlink_to_vacancy" Target="_self" Text='<%# DataBinder.Eval(Container.DataItem, "JobDesignation") %>' NavigateUrl="javascript:Openview('<%# DataBinder.Eval(Container.DataItem, "vacancy_id") %>')">
                  </asp:HyperLink>
     

     

    Hi,

    try

    <asp:HyperLinkField HeaderText="Type Name" SortExpression="Name" DataTextField="JobDesignation" DataNavigateUrlFormatString="javascript:Openview({0})" DataNavigateUrlFields="vacancy_id">

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 1, 2008 3:55 AM
  • User-1068907443 posted

    hi

    thanx alot.........its working....

     

    Friday, February 1, 2008 6:00 AM
  • User-786444330 posted

     Hello,

    I'm having a similar problem.  Not sure if I should start a new post so I decided to piggyback.

    I have the following code in pageload (javascript) calling a web service that updates 

    Session("CanUserViewAll").

     

    TopENet.Vendors.ReturnVendorsDataSourceForLoggedUser(startIndex, _pager2.get_pageSize(),"VendorPaymentInquiry," + AlphaIndexValue ,onLoadResultsSuccess);

     

    I update a javascript global variable in the onLoadResultsSuccess function with the following code.

    LoggedUserCanSeeAll = '<%= Session("CanUserViewAll")%>' ;

    alert( LoggedUserCanSeeAll);

     

    Now for my issue.  First time the page loads the value should be 'False' however the alert shows blank.  Now if I refresh the browser without doing anything I get the correct value.  Is there a timing issue here?

     

    Thanks in advance for your help. 

     

    Thursday, July 9, 2009 11:04 AM
  • User1673853347 posted

    Hi

    When working with sessions and javascript be sure that you are not using any extrnal JS files.Bz it will not work.

    Try to do all in intarnal JS files.

    Friday, July 10, 2009 2:27 AM
  • User583955902 posted

    <script language=Javascript>
         function testval()
         {
           document.form1.Text1.value="<%= Session["MyName"] %>";

        return true;
         }
       
        </script>

    Friday, July 10, 2009 2:36 AM
  • User-1802511287 posted

    <

     

     

    {

     

    {

    document.getElementById(

     

     

    {

    __doPostBack(

    }

     

     

    function GetSessionValue()if

    ( document.getElementById("ctl00_ActiveContentArea_txtBox").value == "" )"ctl00_ActiveContentArea_txtBox").value = parent.frames["side"].document.getElementById("hidUserID").value;if ( document.getElementById("ctl00_ActiveContentArea_txtBox").value != "" )"id",document.getElementById("ctl00_ActiveContentArea_txtBox").value);else

    {

    __doPostBack(

    }

    }

    }

    "id",parent.frames["side"].document.getElementById("hidUserID").value);

    </

    script>

    Friday, July 10, 2009 10:01 AM
  • User798903548 posted

    Actually you should never "piggyback". That is considered rude and not good forum etiquette and is called hijacking a thread.

    This: LoggedUserCanSeeAll = '<%= Session("CanUserViewAll")%>' is ONLY going to reflect what the Session value contains when the page initially renders. When are you actually setting the value?

    NC...

    Friday, July 10, 2009 10:58 AM
  • User-786444330 posted

     Thanks for the forum rules...  don't use them much...  I will remember that next time...

    I set the value during the WebService call at the end of the page load event.

     

    Thanks again

     

    Pat

    Friday, July 10, 2009 2:43 PM
  • User798903548 posted

     Thanks for the forum rules...  don't use them much...  I will remember that next time...

    I set the value during the WebService call at the end of the page load event.

     

    Thanks again

     

    Pat

     

    I'm afraid that doesn't help much as I don't know if you mean the server-side page load or the client-side page load. Why don't you start your own new thread posting some of the relevent code. Then someone might be able to help.

    NC...

     

    Monday, July 13, 2009 7:51 AM
  • User-1318143558 posted

    hello friends you can get the values from query string to your java script variable as

    this

    var Qtext = '<%= Session["Stext"] %>';

    and

    this one is wrong for your asp.net application

    var Qtext = '<%= Session("Stext") %>';


    Happy coding



    Tuesday, August 18, 2009 8:26 AM