Click here to Skip to main content
15,510,771 members
Please Sign up or sign in to vote.
4.40/5 (5 votes)
See more:
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.
JavaScript
<%Session["Test"] = "Welcome Mamu";%>
var session_value='<%=Session["Test"]%>'; 
alert(session_value); 


The above work fine. Note that i have assign Static value(Welcome Mamu). but for Dynamatic,
JavaScript
var strTest=document.getElementById('DropDownList1').value; 
<%Session["Test"] = "'+ strTest +'";%> 
var session_value='<%=Session["Test"]%>';
alert(session_value); 

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?
Posted

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:
JavaScript
 function InitializeRequest(path) {
            // call server side method
            PageMethods.SetDownloadPath(path);
}

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

Must enable page methods set to true
ASP.NET
<asp:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>
 
Share this answer
 
v2
Comments
Member 10766267 10-Nov-14 4:17am    
after run two or more times it will give the value like Empty "" or nothing..
How to Do?
My 1st Page aa.aspx i did
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;
}

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


how i can get strpath value into bb.aspx code behind???
C#
function setJavascript() {
       <% var text1= "Welcome Mamu";%>
           <%Session["status"] =text1;%>
var session_value='<%=Session["status"]%>';
alert(session_value);
        return false;
        }
 
Share this answer
 
Comments
Member 12054008 23-Feb-16 7:13am    
Thanks Rajeev. This code is working fine. Hari A
Instead of using Session["Test"]= '+ strTest +'.;
use this one Session["Test"]=strTest ; because it is member of cs page it directly accessed.
 
Share this answer
 
v2
Comments
Ramanujam Shankar 7-Mar-12 0:24am    
strTest is an variable, can't give Session["Test"]=strTest; directly in javascript
Hi,
store it in a hidden filed, submit it to the server, and retrieve and assign the values in hidden filed to session.
Hope it helps.
Good Luck
 
Share this answer
 
Refer similar thread on CP
session in javascript in asp.net[^]
 
Share this answer
 
 
Share this answer
 
 Use it
< %Session["Test"] = DropDownList1.SelectedValue %>
 
Share this answer
 
Why do you want to assign a session variable from client script???

If you wish to display a message based on a list value do it directly in client script. If you wish to persist it in session, then raise a postback / do it on the subsequent post back. Better yet have a hidden field on you form that you can use to persist this data without adding it to the session.
 
Share this answer
 
Comments
mevenson 24-Dec-18 9:49am    
First why do you need to know why anyone wants to know something. Second, how does your response answer the question? It does not. Instead of providing alternate ideas of how to do a task when you are not aware of all of the requirements, perhaps you should try to provide an answer that pertains to the question. I had the same question, and I needed to do it because once the user clicked away from the page, the data I wanted to save in the Session was gone. I needed to trap the onbeforeunload event in javascript on the page and somehow get the C# code to be able to save any form data in the session so that when the user returned, it was not gone. I did finally figure it out from posts by people who actually answered the question being asked instead of questioning the motives of the person asking the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900