How can I set session variables with JavaScripthttp://forums.asp.net/t/1064192.aspx/1?How+can+I+set+session+variables+with+JavaScriptFri, 18 Nov 2011 02:21:04 -050010641921535373http://forums.asp.net/p/1064192/1535373.aspx/1?How+can+I+set+session+variables+with+JavaScriptHow can I set session variables with JavaScript <p>I would like to set a couple of session variables when I click an image on&nbsp;the page. The session variables will be equal to textboxes on the same&nbsp;page as the image: Here is the tag for the image.</p> <font size="2"> <p></font><font color="#0000ff" size="2">&lt;</font><font color="#a31515" size="2">a</font><font size="2"> </font><font color="#ff0000" size="2">onclick</font><font color="#0000ff" size="2">=&quot;window.open('../../Process.aspx','cal','width=490,height=420,left=500,top=180,scrollbars=no')&quot;</p> </font><font size="2"> <p></font><font color="#ff0000" size="2">href</font><font color="#0000ff" size="2">=&quot;javascript:;&quot;&gt;&lt;</font><font color="#a31515" size="2">img</font><font size="2"> </font><font color="#ff0000" size="2">alt</font><font color="#0000ff" size="2">=&quot;Process&quot;</font><font size="2"> </font><font color="#ff0000" size="2">src</font><font color="#0000ff" size="2">=&quot;../../images/register.gif&quot;</font><font size="2"> </font><font color="#ff0000" size="2">style</font><font color="#0000ff" size="2">=&quot;border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none&quot;</font><font size="2"> </font><font color="#ff0000" size="2">id</font><font color="#0000ff" size="2">=&quot;IMG1&quot;</font><font size="2"> </font><font color="#ff0000" size="2">onclick</font><font color="#0000ff" size="2">=&quot;return IMG1_onclick()&quot;</font><font size="2"> </font><font color="#0000ff" size="2">/&gt;&lt;/</font><font color="#a31515" size="2">a</font><font color="#0000ff" size="2">&gt;</font></p> <font color="#0000ff" size="2"><font color="#0000ff" size="2"> <p>&lt;</font><font color="#a31515" size="2">script</font><font color="#000000" size="2"> </font><font color="#ff0000" size="2">type</font><font color="#0000ff" size="2">=&quot;text/javascript&quot;</font><font color="#000000" size="2"> </font><font color="#ff0000" size="2">src</font><font color="#0000ff" size="2">=&quot;../../NoPostBacks.js&quot;&gt;</p> <p>function</font><font size="2"><font color="#000000"> IMG1_onclick() {</font></font></p> <p><font size="2"><font color="#000000">Session(&quot;Amount&quot;) = Me.txtAmount.Text</font></font></p> <p><font size="2"><font color="#000000">Session(&quot;Zip&quot;) =Me.txtZip.Text</font></p> <p>}</p> </font><font color="#0000ff" size="2"> <p>&lt;/</font><font color="#a31515" size="2">script</font><font color="#0000ff" size="2">&gt;</p> </font></font> 2007-01-17T00:40:44-05:001535429http://forums.asp.net/p/1064192/1535429.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>cant unless you&nbsp; use&nbsp;ajax.</p> <p><a href="http://www.jguru.com/faq/view.jsp?EID=1300197">http://www.jguru.com/faq/view.jsp?EID=1300197</a></p> 2007-01-17T01:59:22-05:001535584http://forums.asp.net/p/1064192/1535584.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript No, sessions can't be accessed&nbsp;by&nbsp;javascript&nbsp;directly.&nbsp;It's&nbsp;stored&nbsp;on&nbsp;server,&nbsp;while&nbsp;javascript&nbsp;is&nbsp;running&nbsp;on&nbsp;client.<br> But can be done indirectly, e.g., store it in a hidden filed, submit it to the server, and retrieve and assign the values in hidden filed to session.<br> Hope it helps.<br> 2007-01-17T05:13:07-05:001536118http://forums.asp.net/p/1064192/1536118.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>Is it possible to pass the values of a couple of textboxes from one form to another without using querystring, session, ajax, or java?</p> <p>What Ineed is&nbsp;when a&nbsp;page opens it&nbsp;invisiably gets the value of a couple of textboxes from the previous page.</p> <p>I appreciate any suggestions.</p> 2007-01-17T12:54:07-05:001537188http://forums.asp.net/p/1064192/1537188.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript There is a new feature called cross page postback in asp.net 2.0, it may meet your need.<br> <a href="http://aspalliance.com/1040_Working_with_Cross_Page_Posting_Using_ASPNET_20">Working with Cross Page Posting Using ASP.NET 2.0</a><br> 2007-01-18T00:05:17-05:002417398http://forums.asp.net/p/1064192/2417398.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>Hi Jack,</p> <p>I discovered that client and server side got communicated better than what I was originally thinking, by using Response and Request object, web app maintences client and server data integrity very well. I do verified that on server side when you want to read client data, e.g. a cookie, you can use Request.Cookie; and vise versa when you want to set a client side cookie, use Response.Cookie.</p> <p>&nbsp;So i dare this assumption to session, and did following client side trying in Javasript, amazingly found, it succeeded. <br> </p> <p>&lt;%Session[&quot;tbActionItem&quot; &#43; &quot;1&quot;] = &quot;abc&quot;; %&gt; </p> <p>However, I am stocked when I use:&nbsp;</p> <p>&lt;%Session[&quot;tbActionItem&quot; &#43; &quot;(i &#43; 1)&quot;] = &quot;abc&quot;; %&gt;&nbsp; // i is a var in the javascript.</p> <p>So my question is how to involve a javascript variable in a inbed session index?&nbsp;</p> <p>&nbsp;Anyone get answer? <br> &nbsp;</p> 2008-06-11T21:08:15-04:002748021http://forums.asp.net/p/1064192/2748021.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>here you go</p> &nbsp;<font color="#0000ff" size="2">var</font><font size="2"> myvar = </font><font color="#a31515" size="2">&quot;Hey Buddy&quot;</font><font size="2">;</font><font size="2"> <p></font><font color="#a31515" size="2">'&lt;%Session[&quot;temp&quot;] = &quot;'</font><font size="2"> &#43; myvar &#43;</font><font color="#a31515" size="2">'&quot;; %&gt;'</font><font size="2"> ;</p> <p>alert(</font><font color="#a31515" size="2">'&lt;%=Session[&quot;temp&quot;] %&gt;'</font><font size="2">);</font></p> <p><font size="2">&nbsp;Happy Programming...!</font><font size="2"></p> </font> 2008-11-15T17:59:02-05:002748183http://forums.asp.net/p/1064192/2748183.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <blockquote><span class="icon-blockquote"></span> <h4>MuhammadHassan</h4> <font color="#0000ff" size="2">var</font><font size="2"> myvar = </font><font color="#a31515" size="2">&quot;Hey Buddy&quot;</font><font size="2">;</font><font size="2"> </font><font size="2"> <p></font><font color="#a31515" size="2">'&lt;%Session[&quot;temp&quot;] = &quot;'</font><font size="2"> &#43; myvar &#43;</font><font color="#a31515" size="2">'&quot;; %&gt;'</font><font size="2"> ;</p> <p>alert(</font><font color="#a31515" size="2">'&lt;%=Session[&quot;temp&quot;] %&gt;'</font><font size="2">);</font></p> <p></p> </blockquote> <p></p> <p>Does this really work?&nbsp; the javascript is executing on the client side.</p> 2008-11-15T22:55:09-05:002748336http://forums.asp.net/p/1064192/2748336.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>yeeeh it works for me why dont you run it. Make sure to Mark as answer if it works for you too.</p> <p>&nbsp;</p> <p>Thank you</p> 2008-11-16T04:32:18-05:003164547http://forums.asp.net/p/1064192/3164547.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>no realy it is not working like a session it is just working like normal variable.</p> <p>i need to set session value in&nbsp; one&nbsp;js method and use it on body load of the next page </p> <p>i am doing this&nbsp;on master page for making tab selected for&nbsp;current &nbsp;page&nbsp;</p> 2009-05-15T07:41:40-04:003354244http://forums.asp.net/p/1064192/3354244.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>Hi <b>rajender,</b></p> <p><br> <b>I am facing the same problem..if u have the code please give me..</b></p> <p><br> </p> <p><b>Regards</b></p> <p><b>Sahaya Arul Sekar.K<br> </b></p> 2009-08-18T14:43:51-04:004013120http://forums.asp.net/p/1064192/4013120.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>i dont know how it works for you!</p> <p>i am also using the same code but i am getting the variable as <b>string</b> not the actual value.</p> <p><br> </p> <p>Any help is appreciated<br> </p> 2010-08-06T08:38:55-04:004014066http://forums.asp.net/p/1064192/4014066.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>Yes, you can totally do it. </p> <p>Here's the recipe for success:&nbsp;</p> <ul> <li>Add an ASP.NET AJAX ScriptManager to your page. </li><li>Create a simple ASMX Web service using Visual Studio. </li><li>Uncomment the Attribute directly above the class definition that reads <strong> [System.Web.Script.Services.ScriptService]</strong> to enable ASP.NET AJAX to see your web methods. </li><li>Create a method to get or update data in your session, and&nbsp;make&nbsp;sure the Attribute above the web method reads <strong>[WebMethod(EnableSession = true)]</strong> so you can have access to the session via your web service method. </li><li>Add a &lt;Services&gt; section to your ScriptManager. </li><li>In the &lt;Services&gt; section add a &lt;ServiceReference&gt; element&nbsp;with the Path attribute set to the path of your .asmx page.</li></ul> <p>OK, so that enables the ability to access your session from the client side!</p> <p>But here's the <strong>really awesome part... </strong>if you include a reference to the web service in your .js file like so:&nbsp;</p> <pre class="prettyprint">///&lt;reference path=&quot;~/MyService.asmx&quot; /&gt;</pre> <p>&nbsp;</p> <p>...you will get <strong>IntelliSense&nbsp;in the JavaScript </strong>for your web service methods! it's very cool!</p> <p>So then you just call your web service methods like you would any other JavaScript function. It's totally easy and almost magical. It's one of my favorite features of ASP.NET AJAX.</p> <p>&nbsp;</p> <p>Hope this helps!</p> <p>&nbsp;</p> <p>EDIT: Oh I should mention that not only <strong>can </strong>you do it, you will actually <strong>love </strong>doing it after you get used to the process!</p> <p>&nbsp;</p> <p>&nbsp;</p> 2010-08-06T18:33:13-04:004016562http://forums.asp.net/p/1064192/4016562.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p><b>Hey evankstone,</b></p> <p><b>thats awesome, it worked. </b></p> <p><b>thats what i call a reply on forums.</b></p> <p><i>thanks a lot.</i><br> </p> <p><br> </p> <p><br> </p> 2010-08-09T05:51:07-04:004017808http://forums.asp.net/p/1064192/4017808.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>Great!!! I'm really glad it worked for you!</p> <p><br> </p> 2010-08-09T16:54:19-04:004171753http://forums.asp.net/p/1064192/4171753.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>Am following the same code, but when I access&nbsp;the webservice function inside my .js file, its returning&nbsp; &quot;undefined&quot; .How will I&nbsp;include a reference to the web service in&nbsp;the .js file so I can debug? Please lemme know</p> 2010-11-16T20:27:36-05:004172737http://forums.asp.net/p/1064192/4172737.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>I see that this thread is three years old and that we're probably just discussion this of academic interest, but really the answer was posted in 2007 by Raymond Wen: Cross page posting. The need for Ajax only arise if there is a requirement that the values are stored instantly upon editing, and not only on navigating to the next page. OP does not say anything about this.</p> 2010-11-17T13:02:02-05:004393594http://forums.asp.net/p/1064192/4393594.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>Hi! </p> <p>&nbsp;</p> <p>i tried this.........</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;Default.aspx.cs&quot; Inherits=&quot;WebApplication1._Default&quot; %&gt;<br> <br> &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br> <br> &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;<br> &lt;head runat=&quot;server&quot;&gt;<br> &nbsp;&nbsp;&nbsp; &lt;title&gt;&lt;/title&gt;<br> &nbsp;&nbsp;&nbsp; &lt;script type=&quot;text/javascript&quot;&gt;<br> <br> &nbsp;&nbsp;&nbsp; function OpenReconcile(referenceNumber) {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert('&lt;%=Session[&quot;temp&quot;] %&gt;');<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var myvar = &quot;Divyesh&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '&lt;%Session[&quot;temp&quot;] = &quot;' &#43; myvar &#43; '&quot;; %&gt;';<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert('&lt;%=Session[&quot;temp&quot;] %&gt;');<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; }<br> &lt;/script&gt;<br> <br> &lt;/head&gt;<br> &lt;body&gt;<br> &nbsp;&nbsp;&nbsp; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;<br> &nbsp;&nbsp;&nbsp; &lt;div&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:TextBox ID=&quot;TextBox2&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;div/&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Button JavaScricpt&quot; OnClientClick=&quot;OpenReconcile('referenceNumber'); return false;&quot; /&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Button ID=&quot;Button2&quot; runat=&quot;server&quot; Text=&quot;Button Only Text&quot; OnClick=&quot;button2click&quot; /&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Button ID=&quot;Button3&quot; runat=&quot;server&quot; Text=&quot;Button Set Session&quot; OnClick=&quot;button3Click&quot; /&gt;<br> &nbsp;&nbsp;&nbsp; &lt;/div&gt;<br> &nbsp;&nbsp;&nbsp; &lt;/form&gt;<br> &lt;/body&gt;<br> &lt;/html&gt;</p> <p>&nbsp;</p> <p>Code behind..........</p> <p>&nbsp;</p> <p>using System;<br> using System.Collections.Generic;<br> using System.Linq;<br> using System.Web;<br> using System.Web.UI;<br> using System.Web.UI.WebControls;<br> <br> namespace WebApplication1<br> {<br> &nbsp;&nbsp;&nbsp; public partial class _Default : System.Web.UI.Page<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void Page_Load(object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void button2click(object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextBox1.Text = Session[&quot;temp&quot;].ToString();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void button3Click(object sender, EventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextBox2.Text = Session[&quot;temp&quot;].ToString();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Session[&quot;temp&quot;] = &quot;Mitesh&quot;;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextBox1.Text = Session[&quot;temp&quot;].ToString();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; }<br> }</p> <p>&nbsp;</p> <p>I tried this because I wanted to access session variable set from client side in to my code behind, but it didnt work. I dont know what am I doing wrong? but I was able to set session vriable in code behind and access it on client side, but the session variable set on client side, shoe me as - ' &#43; myvar &#43; ' I dont know why?</p> <p>&nbsp;</p> <p>can you please reply me back with the solution?</p> <p>&nbsp;</p> <p>Thanks in advance.</p> <p>&nbsp;</p> <p>&nbsp;</p> 2011-04-23T00:17:52-04:004545076http://forums.asp.net/p/1064192/4545076.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>There is no impossible, anything can be done<br> <br> i set session from javascript by very semple way !<br> <br> step 1<br> <br> create transitional file to call any function on server<br> <br> let's<br> <br> 1- create file with tis name ( trnsitional_file.php ) you can change it to any name but my example like this<br> <br> copy this code into it and save<br> <br> code--------------------------start of phpt code</p> <pre class="prettyprint">&lt;?php session_start(); $_SESSION['js_var'] =$_GET['js_var']; ?&gt; session value is = &lt;?php echo $_SESSION['js_var']; ?&gt;</pre> <pre class="prettyprint">&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; function SET_SESSION(SESSION_VALUE){ var url = '&lt;iframe src="trnsitional_file.php?js_var='+SESSION_VALUE+'"&gt;&lt;/iframe&gt;'; document.getElementById("invisible_div").innerHTML=url; } SET_SESSION("am SESSION value"); &lt;/script&gt; &lt;/head&gt; &lt;body &gt; &lt;input type="button" value="SET SET SESSION" onClick='SET_SESSION("NEW SESSION ONCLICK")'&gt; &lt;div id="invisible_div" style="width:600px; height:200px; visibility:visible"&gt;&lt;/div&gt; &lt;script type="text/javascript"&gt; SET_SESSION("ANY THING"); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;<br /><br /></pre> <pre class="prettyprint">code--------------------------end of php code 2-create anther html file in any name or just copy function to you file copy this javascript to your file don't forget to create DIV in your body you can make it hidden. code--------------------------start of javascript code code-------------------------- end of js code you can use this function any where sorry for my english! Engineer Yahya Al-Salmani Saudi Ariba </pre> 2011-08-09T00:15:40-04:004688992http://forums.asp.net/p/1064192/4688992.aspx/1?Re+How+can+I+set+session+variables+with+JavaScriptRe: How can I set session variables with JavaScript <p>I simply use</p> <pre style="background:white; color:black; font-family:Courier New"><span style="background:yellow">&lt;%</span>Session[<span style="color:maroon">&quot;aux1_7&quot;</span>]=<span style="color:maroon">&quot;7&quot;</span>;<span style="background:yellow">%&gt;</span>;</pre> <pre style="background:white; color:black; font-family:Courier New">I use this within a Javascript method (it will work anywhere in Javascript).</pre> <pre style="background:white; color:black; font-family:Courier New">Make sure that both semicolons are there (one for C# the other for Javascript).</pre> <pre style="background:white; color:black; font-family:Courier New">The reason this works is because anything between the &quot;&lt;%&quot; and &quot;%&gt;&quot; runs at the server</pre> <pre style="background:white; color:black; font-family:Courier New">thus creating a session variable in this case.</pre> <pre style="background:white; color:black; font-family:Courier New">&nbsp;</pre> 2011-11-18T02:21:04-05:00