Using PageMethods/WebMethods to update Sessions variable
Posted by malakablog on October 8, 2008
I created functions for updateing/reading sessions variables using PageMthods and WebMethods, I am using VS2005 (VB.NET):
Import the web.services library
Imports System.Web.Services
This is the VB.NET function for setting the session variables
<WebMethod()> _
Public Shared Function AjaxSetSession(ByVal SessionValue As String)
Try
HttpContext.Current.Session(“SessionKey”) = SessionValue
Catch ex As Exception
End Try
End Function
To access the session we need to use HttpContext.Current.Session
This is the VB.NET function for getting the session variables
<WebMethod()> _
Public Shared Function AjaxGetSession() As String
Try
AjaxGetSession = HttpContext.Current.Session(“SessionKey”)
Catch ex As Exception
AjaxGetSession = “Error”
End Try
End Function
This is the JavaScript Code to set the session variable
PageMethods.AjaxSetSession(“PageMethods”,”WebMethods”)
This the JavaScript Code to read the Session Variables
PageMethods.AjaxGetSession(JSGetSessionSucess,JSGetSessionFaild);
JSGetSessionSucess is a JavaScript function that would be executed if the AjaxGetSession succeeded
function JSGetSessionSucess(value, methodName)
{
try
{
alert(value);
}
catch(err)
{
}
finally
{
}
}
JSGetSessionFaild is a JavaScript function that would be executed if the AjaxGetSession failed
function JSGetSessionFaild (ex, methodName)
{
try
{
alert(ex.get_exceptionType());
}
catch(err)
{
}
finally
{
}
}






