JavaScript-如何在JavaScript中为会话设置值 [英] JavaScript - How to Set values to Session in Javascript

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

问题描述


我想在会话中设置下拉列表选择的值.我已经在后面的代码中做了.但是对于某些情况,我必须自己在客户端执行.我尝试了以下.但是我还没有解决办法.

Hi,
I want to set the Dropdown selected value in Session. I have done in Code behind. But for some condition i have to Do in Client Side Itself. i tried the following. but i did''t solution yet.

<%Session["Test"] = "Welcome Mamu";%>
var session_value='<%=Session["Test"]%>'; 
alert(session_value); 



以上工作正常.请注意,我已分配了静态值(欢迎使用Mamu).但对于Dynamatic,



The above work fine. Note that i have assign Static value(Welcome Mamu). but for Dynamatic,

var strTest=document.getElementById('DropDownList1').value; 
<%Session["Test"] = "'+ strTest +'";%> 
var session_value='<%=Session["Test"]%>';
alert(session_value); 


在客户端运行正常.但是在服务器端(后台代码)中,Session ["Test"]的值为''+ strTest +''.


It is Working fine in Client Side. But i Server Side(Code Behind), the Session["Test"] value is ''+ strTest +''.

Is any other way to assign values to Session?

推荐答案

无法直接通过javascript直接分配会话值.

我找到了其他方法.调用函数背后的代码并分配会话值.

Javascript函数:
Not possible to assign session values directly through javascript.

I found alternative ways. Call the code behind function and assign the session values.

Javascript Function:
 function InitializeRequest(path) {
            // call server side method
            PageMethods.SetDownloadPath(path);
}


函数背后的代码:

[System.Web.Services.WebMethod]
    public static string SetDownloadPath(string strpath)
    {
        Page objp = new Page();
        objp.Session["strDwnPath"] = strpath; 
        return strpath;
    }


必须将页面方法设置为true

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


function setJavascript() {
       <% var text1= "Welcome Mamu";%>
           <%Session["status"] =text1;%>
var session_value='<%=Session["status"]%>';
alert(session_value);
        return false;
        }


而不是使用Session["Test"]= ''+ strTest +''.;
使用此Session["Test"]=strTest ;,因为它是直接访问的cs页面的成员.
Instead of using Session["Test"]= ''+ strTest +''.;
use this one Session["Test"]=strTest ; because it is member of cs page it directly accessed.


这篇关于JavaScript-如何在JavaScript中为会话设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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