JavaScript - How to Set values to Session in Javascript

Asked By Er Suresh Ganesan on 18-Jan-09 01:20 PM

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 +'";%>

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?

You could create a hidden element

Venkat K replied to Er Suresh Ganesan on 18-Jan-09 09:24 PM
 in the ASPX generated page and assign the value of your session variable to one of the element's attributes. The element attribute can then be retrieved from within a JavaScript function.

E.g. (Assuming VBScript) If you have a form on your page, add the following to the ASP code which generates the page:

Response.Write("<input type='hidden' name='fred' value='" & Session("VariableName") & "'>")

In your JavaScript function, find the form element with the name == "fred" and retrieve the value.

hi

alice johnson replied to Er Suresh Ganesan on 18-Jan-09 09:53 PM

hey,

you can do it the in the code behind , its more better:

To it this way:

Session["item"]=Dropdownlist1.Selecteditem.Tostring();


re

Web Star replied to Er Suresh Ganesan on 18-Jan-09 11:19 PM

u r right u can use in client side as

client_side_var = "<%=session("var")%>"

in server side

Session["Test"]  =  strTest;

Thanks
Er Suresh Ganesan replied to Venkat K on 18-Jan-09 11:38 PM

Hi Rajesh Kallakuri,

            thanks for ur reply. but i have to assign in Client side itself. i mean, i have to assign Dropdownlist selected value in the session.

Thanks
Er Suresh Ganesan replied to alice johnson on 18-Jan-09 11:39 PM

Hi alice johnson ,

            thanks for ur reply. but i have to assign in Client side itself. i mean, i have to assign Dropdownlist selected value in the session.

Thanks
Er Suresh Ganesan replied to Web Star on 18-Jan-09 11:40 PM

Hi A K ,

      thanks for ur reply. but i have to assign in Client side itself. i mean, i have to assign Dropdownlist selected value in the session.

`How to Set values to Session in Javascript
mv ark replied to Er Suresh Ganesan on 19-Jan-09 03:01 AM
You cannot directly set a session variable via JavaScript.

HTTP is a stateless protocol. Each command is executed independently, without any knowledge of the commands that came before it. The Javascript will execute only after the page has rendered and loaded completely.

This line takes effect only AFTER the page has loaded -
var strTest=document.getElementById('DropDownList1').value;

whereas this line would have executed WHILE the page was getting rendered-
<%Session["Test"] = "'+ strTest +'";%>

Due to the different times at which the lines get executed, you will not be able to achieve the functionality you are looking for.

You would have to persist the value that you want to be store inside a Session within a cookie or hidden variable or querystring & fetch it in the next page after it is submitted.

Alternatively, you can use AJAX - http://aselai.blogspot.com/2008/07/set-aspnet-session-variables-with.html
sdfsdf replied to mv ark on 25-Mar-10 11:09 AM
var a;
            a = "";            
           '<%Session["FsbsTimer"]= " '+ a + '" ;%>'
Akheel replied to Er Suresh Ganesan on 21-May-10 04:49 AM
<%--<%Session["SanWardNo"]=txtSanwardNo.Text;%>--%>

where txtSanwardno is a serverside text box
help
Hi all, I am developing a web application using asp.net. my monitor size is "1448x880" (width = 1448px and height = 880px). I have designed a page contents in any resolutions? Thanks R. Eswaran. You can find the screen resolution easily from javascript and accordeing to that you can design your website; see this code to find resolution Page_Load(Object sender, EventArgs e){ if (Request.QueryString["action"] ! = null) { / / store the screen resolution in Session["ScreenResolution"] / / and redirect back to default.aspx Session["ScreenResolution"] = Request.QueryString["res"].ToString(); Response.Redirect
how to use message box in asp.net with c#. In message box , If i click Ok it should do some actions, If message'); < / script> "); hope this helps. . . :-) Hi, you cannot use windows forms like message boxes in asp.net. You can go with confrim box of Javascript. use like if(confirm("Are you sure
Hi. I'm working on Peter A. Bromberg's article Preventing Multiple Logins in ASP.NET. My issue is that if someone closes his browser his session doesn't expire. Then he wants to go back in again. It's not going is, use a Global.asax file that contains event for the lifecycle of the page / session. Use the event application-end to track when the application is closed and force the
print using asp.net ASP.NET 06-Jun-13 10:06 AM i created one asp.net application on my page i use one button as print. . . when i click print button Text = "New Entry" OnClientClick = "window.open('~ / email.asxp');" / > For print: Untitled document < script type = "text / javascript"> function CallPrint (strid) { var prtContent = document.getElementById(strid); var WinPrint = window.open( '' , '' , 'letf = 0, top you want to print- - %> < / div > < input type = "button" value = "print " id = "btnPrint" runat = "Server" onclick = "javascript: CallPrint ('divPrint')" / > its really easy to print a page using a simple link on a
How to create upload progress bar in asp.net with c#. http: / / www.codeproject.com / KB / webforms / File_Upload_Progress_Bar.aspx hi, See this link n html> <head> <title> File Upload Ajax Example < / title> <script src = "script / main.js" type = "text / javascript"> < / script> <link rel = "stylesheet" href = " / css / main.css" type = "text / css"> { . . . see src code for some simple javascript / css removed here for space . . . } <script> function process() { toggleButton('submit_button'); startpage_checkUploadStatus(); return true; } < / script> < / head
Hi folks, I am in need to implement Single Sign on for a group of asp.net web applications. So I went through few articles and have done the following. . I use Yes, but I think your application also uses Validators and other .Net components that embed javascript code. 2. If Cookies are the only thing needed, then will it be secure secure? Encrypted cookies are safe, and even asp.net authentication uses it. 3. Will it be scalable for 5000 users at an instance
how to disable browser back button using asp.net? Hi Yes you can do by two ways 1. Javascript add this script in your web page head(html). <script type = "text javascript" > function disableBackButton() { window.history.forward(-1); } < / script 2. by C# protected void Page_Init(object Sender
can anybody give me example how I can return of objects) in a view (single web page) from controller? Thank you in advance. . HI ASP.NET MVC 2 We shipped ASP.NET MVC 1.0 last March. Since then, almost 1 million developers have downloaded and used
Auto logout in asp.net In ASP.NET this is some thing like hard. But for the Forms Authentication, you can set time
hi what is the difference between if(isPostback) and if(!isPostback) I am getting confused. . pls explain me thx in advance In ASP.Net, the Page Load is event is fired every time a page is loaded. This is submitted) back to the same page that contains it, it's called a post back. ASP.NET provides a property called IsPostBack that is TRUE when the page is being loaded as