Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET 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); 

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 +'";%>
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 6-Mar-12 18:19pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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);
}
Code Behind Function:
[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:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>
  Permalink  
v2
Comments
Member 10766267 at 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???
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Instead of using Session["Test"]= '+ strTest +'.;
use this one Session["Test"]=strTest ; because it is member of cs page it directly accessed.
  Permalink  
v2
Comments
Ramanujam Shankar at 7-Mar-12 0:24am
   
strTest is an variable, can't give Session["Test"]=strTest; directly in javascript
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Refer similar thread on CP
session in javascript in asp.net[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

 Use it
< %Session["Test"] = DropDownList1.SelectedValue %>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

function setJavascript() {
       <% var text1= "Welcome Mamu";%>
           <%Session["status"] =text1;%>
var session_value='<%=Session["status"]%>';
alert(session_value);
        return false;
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 249
2 Sergey Alexandrovich Kryukov 205
3 Aajmot Sk 197
4 Sinisa Hajnal 176
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100