加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用javascript获取会话值

发布时间:2020-12-15 17:45:09 所属栏目:百科 来源:网络整理
导读:我有一个类来处理会话变量.以下是附件样本: namespace General{ public class Session { public Session() { } public static string UserID { get { return HttpContext.Current.Session["UserID"] as string ?? String.Empty; } set { HttpContext.Curren
我有一个类来处理会话变量.以下是附件样本:
namespace General
{

    public class Session
    {
        public Session()
        {
        }


        public static string UserID
        {
            get { return HttpContext.Current.Session["UserID"] as string ?? String.Empty; }
            set { HttpContext.Current.Session["UserID"] = value;  }
        }

        public static string departFlightID
        {
            get { return HttpContext.Current.Session["departFlightID"] as string ?? String.Empty; }
            set { HttpContext.Current.Session["departFlightID"] = value; }
        }

        public static string returnFlightID
        {
            get { return HttpContext.Current.Session["returnFlightID"] as string ?? String.Empty; }
            set { HttpContext.Current.Session["returnFlightID"] = value; }
        }
}

}

现在在某个时候我将flightID存储在:

General.Session.departFlightID = flightID;

而在另一点我想使用Javascript检索这个值.我已经看到这里的例子,但他们不会工作(没有错误,但他们将返回EMPTY).

最近的尝试:

var session = '<%= General.Session.departFlightID %>';
 var session = '<%= HttpContext.Current.Session["departFlightID"] %>';

它的工作,如果我设置页面加载的价值,但我正在运行一个webmethod,我创建一个html,然后发送该html回来填充在一个div,在那里打票.我需要获取会话值,但不会更新.

为了更清楚这里是Javascript代码:

<script type="text/javascript">


function addTicket(flightID,flightType) {
    PageMethods.addTicket(flightID,flightType,OnGetMessageSuccess);
}

function OnGetMessageSuccess(result,userContext,methodName) {

    var pageUrl = document.URL;
    if (pageUrl.indexOf("&ret_date=&") !== -1) {

        var session = '<%= HttpContext.Current.Session["departFlightID"] %>';

        alert(session);
        result = result + "<a onclick="searchflight.abortAllAjax()" data-index="6" class="bookNow" title="Book Now!" href="#">Book Now!</a>";
        result = result + "</div>    </div>  </div>";
        document.getElementById("detail").innerHTML = result;
    }

}

这里是webmethod:

[System.Web.Services.WebMethod]
    public static string addTicket(string flightID,string flightType)
    {
        //GET FLIGHT DETAILS  FROM DATATABLE OR SESSION DATA TABLE
        string tmpHtml = string.Empty;
        tmphtml="sample";
        string flightID="123123213";
        General.Session.departFlightID=flightID;

    return tmphtml;
    }

解决方法

这是和 here一样的问题.

我们通常在会话变量中具有敏感数据,因此它们总是服务器端.仍然如果你想你可以尝试添加一个HiddenField在你的.aspx标记和代码后面你可以设置它.一旦设置了值,您可以使用$(“#id”)方式或Document.GetElementById()方法轻松访问该值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读