Create a simple ASMX Web service using Visual Studio.
Uncomment the Attribute directly above the class definition that reads
[System.Web.Script.Services.ScriptService] to enable ASP.NET AJAX to see your web methods.
Create a method to get or update data in your session, and make sure the Attribute above the web method reads
[WebMethod(EnableSession = true)] so you can have access to the session via your web service method.
Add a <Services> section to your ScriptManager.
In the <Services> section add a <ServiceReference> element with the Path attribute set to the path of your .asmx page.
OK, so that enables the ability to access your session from the client side!
But here's the really awesome part... if you include a reference to the web service in your .js file like so:
///<reference path="~/MyService.asmx" />
...you will get IntelliSense in the JavaScript for your web service methods! it's very cool!
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.
Hope this helps!
EDIT: Oh I should mention that not only can you do it, you will actually
love doing it after you get used to the process!
evan k. stone | sr. .NET web developer | sf bay area, ca | twitter: interactivlogic (no "e." twitter only allows 15 chars.)
Am following the same code, but when I access the webservice function inside my .js file, its returning "undefined" .How will I include a reference to the web service in the .js file so I can debug? Please lemme know
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.
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 - ' + myvar + ' I dont know why?
can you please reply me back with the solution?
Thanks in advance.
Mitesh Patel.
Microsoft Corporation.
Redmond, Washington.
United States.
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
sekar84
Member
3 Points
1 Post
Re: How can I set session variables with JavaScript
Aug 18, 2009 02:43 PM|LINK
Hi rajender,
I am facing the same problem..if u have the code please give me..
Regards
Sahaya Arul Sekar.K
vijay.bhosal...
Member
6 Points
5 Posts
Re: How can I set session variables with JavaScript
Aug 06, 2010 08:38 AM|LINK
i dont know how it works for you!
i am also using the same code but i am getting the variable as string not the actual value.
Any help is appreciated
evankstone
Member
54 Points
36 Posts
Re: How can I set session variables with JavaScript
Aug 06, 2010 06:33 PM|LINK
Yes, you can totally do it.
Here's the recipe for success:
OK, so that enables the ability to access your session from the client side!
But here's the really awesome part... if you include a reference to the web service in your .js file like so:
...you will get IntelliSense in the JavaScript for your web service methods! it's very cool!
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.
Hope this helps!
EDIT: Oh I should mention that not only can you do it, you will actually love doing it after you get used to the process!
vijay.bhosal...
Member
6 Points
5 Posts
Re: How can I set session variables with JavaScript
Aug 09, 2010 05:51 AM|LINK
Hey evankstone,
thats awesome, it worked.
thats what i call a reply on forums.
thanks a lot.
evankstone
Member
54 Points
36 Posts
Re: How can I set session variables with JavaScript
Aug 09, 2010 04:54 PM|LINK
Great!!! I'm really glad it worked for you!
swapna56
Member
39 Points
27 Posts
Re: How can I set session variables with JavaScript
Nov 16, 2010 08:27 PM|LINK
Am following the same code, but when I access the webservice function inside my .js file, its returning "undefined" .How will I include a reference to the web service in the .js file so I can debug? Please lemme know
Nilzor
Member
50 Points
29 Posts
Re: How can I set session variables with JavaScript
Nov 17, 2010 01:02 PM|LINK
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.
mtp2408
Member
4 Points
3 Posts
Re: How can I set session variables with JavaScript
Apr 23, 2011 12:17 AM|LINK
Hi!
i tried this.........
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
function OpenReconcile(referenceNumber) {
alert('<%=Session["temp"] %>');
var myvar = "Divyesh";
'<%Session["temp"] = "' + myvar + '"; %>';
alert('<%=Session["temp"] %>');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<div/>
<asp:Button ID="Button1" runat="server" Text="Button JavaScricpt" OnClientClick="OpenReconcile('referenceNumber'); return false;" />
<asp:Button ID="Button2" runat="server" Text="Button Only Text" OnClick="button2click" />
<asp:Button ID="Button3" runat="server" Text="Button Set Session" OnClick="button3Click" />
</div>
</form>
</body>
</html>
Code behind..........
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button2click(object sender, EventArgs e)
{
TextBox1.Text = Session["temp"].ToString();
}
protected void button3Click(object sender, EventArgs e)
{
TextBox2.Text = Session["temp"].ToString();
Session["temp"] = "Mitesh";
TextBox1.Text = Session["temp"].ToString();
}
}
}
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 - ' + myvar + ' I dont know why?
can you please reply me back with the solution?
Thanks in advance.
Microsoft Corporation.
Redmond, Washington.
United States.
yahya800
Member
2 Points
1 Post
Re: How can I set session variables with JavaScript
Aug 09, 2011 12:15 AM|LINK
There is no impossible, anything can be done
i set session from javascript by very semple way !
step 1
create transitional file to call any function on server
let's
1- create file with tis name ( trnsitional_file.php ) you can change it to any name but my example like this
copy this code into it and save
code--------------------------start of phpt code
<html> <head> <script type="text/javascript"> function SET_SESSION(SESSION_VALUE){ var url = '<iframe src="trnsitional_file.php?js_var='+SESSION_VALUE+'"></iframe>'; document.getElementById("invisible_div").innerHTML=url; } SET_SESSION("am SESSION value"); </script> </head> <body > <input type="button" value="SET SET SESSION" onClick='SET_SESSION("NEW SESSION ONCLICK")'> <div id="invisible_div" style="width:600px; height:200px; visibility:visible"></div> <script type="text/javascript"> SET_SESSION("ANY THING"); </script> </body> </html>
len5134x
Member
4 Points
2 Posts
Re: How can I set session variables with JavaScript
Nov 18, 2011 02:21 AM|LINK
I simply use