.Net – How to Set values to Session in JavaScript?

How to Set values to Session in JavaScript

Today, we will not posted a blog. It’s a question and answer. Below you will find how to set values to session in JavaScript?

Question –

I want to set the drop down 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 didn’t solution yet.

<%Session["Test"] = "Welcome";%>  
var session_value='<%=Session["Test"]%>';  

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

var strTest=document.getElementById('DropDownList1').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?

Answer –

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

public static string SetDownloadPath(string strpath)
    Page objp = new Page();
    objp.Session["strDwnPath"] = strpath; 
    return strpath;

Must enable page methods set to true

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


Related Blog – Redirect to another URL

Like us on Facebook and Linkedin for more updates.

Back To Top