使用JavaScript在客户端设置会话 [英] Set Session in Client Side using JavaScript

查看:114
本文介绍了使用JavaScript在客户端设置会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助。我的问题是当会话中的赋值无法找到另一页的正确值时。我的代码段是

I need a bit help. My problem is when assign value in session can't find a proper value another page. my code snippet is

var _user = document.getElementById("username").value;
'<%Session["UserName"] = "'+ _user +' "; %>'





当我们得到另一个页面的值时,我发现值为'+ _user +'。如何找到输入值和代码。我的代码背后



when we are get a value another page i find value '+_user+'. How can find enter values and code behind. My code behind

string my = Session["UserName"].ToString();



任何伙伴帮我..


any buddy help me..

推荐答案

您不能通过 JavaScript 会话值c>因为会话服务器端进行管理。



而不是设置会话客户端,您可以调用 WebMethod 并设置会话里面。

You can't set Session value directly in this way by JavaScript because Sessions are managed at Server side.

Instead of setting Session at Client Side, you can call a WebMethod and set the Session inside it.
  1. Code Behind中定义一个 WebMethod
  1. Define one WebMethod in Code Behind like...
[System.Web.Services.WebMethod]
public static void SetUserName(string userName)
{
    Page objPage = new Page();
    objPage.Session["UserName"] = userName;
}

  • 要从 aspx 页面调用此方法,请包含 ScriptManager 启用 Pagemethods

  • To call this method from aspx page, include a ScriptManager to enable Pagemethods.

    <asp:ScriptManager EnablePageMethods="true" ID="ScriptManager" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>



    并编写以下代码......


    And write the following code...

    var _user = document.getElementById("username").value;
    PageMethods.SetUserName(_user);




  • 试试这样在链接中给出,在客户端设置后检查会话值。

    http://shekharshetemcts.wordpress.com/2013/11/27/how-to-access-session-variables-using-javascript-in- asp-net / [ ^ ]

    这将确保您在客户端方面正确设置会话天气
    Hi,
    Try like this given in the link as check the session value after setting it on the client side.
    http://shekharshetemcts.wordpress.com/2013/11/27/how-to-access-session-variables-using-javascript-in-asp-net/[^]
    This will ensures you on the client side that weather the session is set correctly or not


    这篇关于使用JavaScript在客户端设置会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆