session variables, accessing from client side java script

Discussion in 'ASP .Net' started by ray well, Mar 16, 2005.

  1. ray well

    ray well Guest

    hi,

    is a session varibale acessible for reading and writing from client side
    javascript?

    say i have a session variable called 'Session("Color")', and i want to
    read/write to it, how would i refere to it say in a onclick event?

    i would appreciate a snipet of code.

    thanks

    ray
     
    ray well, Mar 16, 2005
    #1
    1. Advertising

  2. ray well

    Marina Guest

    No, it is not accessible from client side javascript. Session information is
    stored on the server.

    "ray well" <> wrote in message
    news:%...
    > hi,
    >
    > is a session varibale acessible for reading and writing from client side
    > javascript?
    >
    > say i have a session variable called 'Session("Color")', and i want to
    > read/write to it, how would i refere to it say in a onclick event?
    >
    > i would appreciate a snipet of code.
    >
    > thanks
    >
    > ray
    >
    >
     
    Marina, Mar 16, 2005
    #2
    1. Advertising

  3. ray well

    Guest

    If you wish to do so then you need to store the information in
    something that is sent to the client side. The easiest way to do this
    is to use an <input type="hidden" id="hdfMyColor" runat="server" />
    then in your codebehind define the variable as such
    or
    Protected WithEvents hdfMyColor As
    System.Web.UI.HtmlControls.HtmlInputHidden


    Attach an event handler to the PreRender method and in there set the
    HtmlInputHidden's value to the session value
    Private Sub Page_Render(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.PreRender
    hdfMyColor.Value = Session("Color")
    End Sub


    now you would simply go
    <script type="text/javascript">
    var colorHolder = document.getElementById("hdfMyColor");
    var color = colorHolder.value;
    </script>
     
    , Mar 16, 2005
    #3
  4. ray well

    Guest

    Another method to doing that is to use RegisterClientScriptBlock to set
    the variable manually through javascript code.


    Private Sub Page_Render(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.PreRender
    RegisterClientScriptBlock("ddd", "<script type='text/javascript'> var
    color = '" + Session("Color") + "';</script>")
    End Sub


    This method may be alot easier
     
    , Mar 16, 2005
    #4
  5. No, Session variables reside on the server. If you want access, you will
    need to postback to the server for your on click event.

    bill

    "ray well" <> wrote in message
    news:%...
    > hi,
    >
    > is a session varibale acessible for reading and writing from client side
    > javascript?
    >
    > say i have a session variable called 'Session("Color")', and i want to
    > read/write to it, how would i refere to it say in a onclick event?
    >
    > i would appreciate a snipet of code.
    >
    > thanks
    >
    > ray
    >
    >
     
    William F. Robertson, Jr., Mar 16, 2005
    #5
  6. ray well

    Guest

    Or use any of my provided methods
     
    , Mar 17, 2005
    #6
  7. How does either of your methods help ray meet his requirements of reading
    and writing a session variable?

    bill


    "" <> wrote in message
    news:...
    > Or use any of my provided methods
    >
     
    William F. Robertson, Jr., Mar 24, 2005
    #7
  8. ray well

    remit2k

    Joined:
    Jul 31, 2007
    Messages:
    1
    accessing session value in client side javascript

    var color='<%= Session("color").ToString() %>'
    this is the simple way to access the session value in javascript
     
    remit2k, Jul 31, 2007
    #8
  9. ray well

    ramgopal

    Joined:
    Nov 15, 2008
    Messages:
    1
    Location:
    India
    javascript + session

    hi,

    you can also use hidden field to store the session value and read that value in javascript.

    for example,

    session["name"]="well";
    hiddenctrl.value=session["name"].ToString();
    client-side:

    javascript:

    var sessionvalue= document.getElementById('hiddenctrl').value;

    hope it helps
     
    ramgopal, Nov 15, 2008
    #9
    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. Craig G
    Replies:
    3
    Views:
    30,847
    Craig G
    Jan 26, 2005
  2. Magnus Blomberg
    Replies:
    3
    Views:
    740
    Magnus Blomberg
    Apr 14, 2005
  3. Boss302
    Replies:
    0
    Views:
    1,097
    Boss302
    Nov 21, 2006
  4. Keithb
    Replies:
    1
    Views:
    821
    bruce barker
    Feb 26, 2007
  5. Ankur
    Replies:
    5
    Views:
    237
    Dr J R Stockton
    Jun 29, 2007
Loading...

Share This Page