Hi Friends,
I had face
following problem regarding maintaining state in the stateless world of HTTP.
- Access Session variable Inside Static methods.
- Access
session variable in web service / web method
- Access
session variable in ASP.Net App_Code
- Get
session variable in jquery / JavaScript
Solutions :
How to access Session variable Inside
Static methods / web service / web method
We can access session using System.Web.SessionState.HttpSessionState class.
By default,
ASP.NET session support for each Web method is turned off. we have to
explicitly
enable
session support for each Web method that wants to use session state.
This is done
by adding the EnableSession property to the WebMethod attribute of your
function.
VB.NET Code:
***********************************************************
VB.NET Code:
<WebMethod(EnableSession:=True)> _
Public Function
SetSessionVar() As Integer
Dim cnt As Integer
If
HttpContext.Current.Session("mysession") Is Nothing Then
cnt = 1
Else
cnt = convert.ToInt16(HttpContext.Current.Session("mysession"))
+ 1
End If
Context.Session("mysession") = cnt
Return cnt
End Function
|
C# Code :
[WebMethod(EnableSession =
true)]
public static int
SetSessionVar()
{
int cnt;
if(HttpContext.Current.Session("mysession") = null )
{ cnt = 1 ;
}
else
{ cnt =
Convert.ToInt16(HttpContext.Current.Session("mysession")) + 1 ;
}
HttpContext.Current.Session("mysession") = cnt ;
return
cnt;
}
|
Solution : Access session variable in ASP.Net App_Code
If you want
to get or set session inside App_Code class file then you must use .
To Set Value to Session variable
HttpContext.Current.Session("mysession") =
"Tarun"
|
To Get Value to Session variable
Myvariable = HttpContext.Current.Session("mysession")
|
Get session variable in j query / JavaScript
var mysessionItem = '<% Session("mysession") %>'
|
No comments:
Post a Comment