Can you access Session Variables from Client Side Script?

Discussion in 'ASP .Net' started by Craig G, Jan 26, 2005.

  1. Craig G

    Craig G Guest

    if so, how?
    Craig G, Jan 26, 2005
    #1
    1. Advertising

  2. No, you have to pass them to client, for example in a hidden input element.

    Eliyahu

    "Craig G" <craig.gamble@y_arrasoftware.com> wrote in message
    news:ur%...
    > if so, how?
    >
    >
    >
    Eliyahu Goldin, Jan 26, 2005
    #2
    1. Advertising

  3. Craig G wrote:
    > if so, how?

    You cannot access Session directly from client side script. However, you
    can generate JavaScript code to assign the value of the Session variable
    to a client side variable. If you change the client side variable,
    you'll have to post the new value back in order to update the Session.

    Here is a simple example which creates a JavaScript variable with the
    value of a Session variable:
    <%@ Page language="c#" AutoEventWireup="true" %>
    <script runat="server">
    void Page_Load()
    {
    Session["MySessionVar"]="Hello world";
    }
    </script>
    <html>
    <script language="JavaScript">
    var mySessionVar="<%= Session["MySessionVar"] %>";
    </script>
    <body>
    <form runat="server"></form>
    </body>
    </html>

    Anders Norås
    http://dotnetjunkies.com/weblog/anoras/
    =?ISO-8859-1?Q?Anders_Nor=E5s?=, Jan 26, 2005
    #3
  4. Craig G

    Craig G Guest

    nice one, cheers for the help!

    "Anders Norås" <> wrote in message
    news:...
    > Craig G wrote:
    > > if so, how?

    > You cannot access Session directly from client side script. However, you
    > can generate JavaScript code to assign the value of the Session variable
    > to a client side variable. If you change the client side variable,
    > you'll have to post the new value back in order to update the Session.
    >
    > Here is a simple example which creates a JavaScript variable with the
    > value of a Session variable:
    > <%@ Page language="c#" AutoEventWireup="true" %>
    > <script runat="server">
    > void Page_Load()
    > {
    > Session["MySessionVar"]="Hello world";
    > }
    > </script>
    > <html>
    > <script language="JavaScript">
    > var mySessionVar="<%= Session["MySessionVar"] %>";
    > </script>
    > <body>
    > <form runat="server"></form>
    > </body>
    > </html>
    >
    > Anders Norås
    > http://dotnetjunkies.com/weblog/anoras/
    Craig G, Jan 26, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ray well
    Replies:
    8
    Views:
    163,386
    ramgopal
    Nov 15, 2008
  2. Magnus Blomberg
    Replies:
    3
    Views:
    707
    Magnus Blomberg
    Apr 14, 2005
  3. Boss302
    Replies:
    0
    Views:
    1,024
    Boss302
    Nov 21, 2006
  4. Keithb
    Replies:
    1
    Views:
    805
    bruce barker
    Feb 26, 2007
  5. Bogdan
    Replies:
    2
    Views:
    639
    Bogdan
    Jun 9, 2008
Loading...

Share This Page