Showing posts with label session. Show all posts
Showing posts with label session. Show all posts

Wednesday, March 28, 2012

Calling from client to server directly

Hi,

i want to access my server session values from the client using Ajax.Net, and i'm unsure how to do that.

does the only way to access the server from the client (without a postback) is using a web service? can i access

the server methods in my code behind file?

if someone can attach some code sample it will be greatly appreciated

This is actually pretty easy to do by taking advantage of the "WebMethods" feature of ASP.NET AJAX.

http://ajax.asp.net/docs/tutorials/ExposingWebServicesToAJAXTutorial.aspx

Notice the last section on "Calling Static Methods". The drawback is that your method will need to be static. Fortunately, you can still grab the session values by using

HttpContext.Current.Session[key]

hth

-Tony


Hi,

thanks, but this is the code i use right now.

for some reason, i can't make it work when the methods marked as [WebMethod] are in the code behind file - only in the code infornt...

is this a known limitation??

thanks


Not sure what you mean by only being able to get this to work in the code infront. Are you sure that your page is set up to use the codebehind file? What does your method declaration look like? Is it possible that your code behind file isn't compiling due to an error?
If you're refering to the codefile for teh page itself, you have to do a couple things. first, the method in the codefile has to be static, and second the scriptmanager has to have EnablePageMethods=true. After that, you should be able to call it from js via PageMethods.MethodName();

Hi,

My code behind file is configured correctly, and i can call other methods in the code behind file.

my method is marked as public static, and has the

[WebMethod] flag on it.

when my method is declared in the ASPX file under <script runat="server"> tags, it works fine

and i can access my server objects , when i copy it into my code behind file inside the class

representing the page, i get a javascipt error saying "PageMethods is undefined"

any ideas??

thanks


Hi shahar_lazer,

Can you post your code over here so we can test and hopefully fix it for you? Thanks

Regards,


I'd start looking in 2 places. Do you have a script manager on the page? If so, do you have EnablePageMethods=true?

The second place you should look is your web.config. Is this a new "AJAX" project, or are you upgrading an older app? The Web.config needs to have certain entries in it to make everything work, including PageMethods. Try creating a new AJAX project and defining and testing a PageMethod there - if that works, you can narrow it down to your app, and likely your config.


Hi,

Tried my code inside an Ajax project and got the same results.

here is my code (basically taken from the site examples)

<%@. Page Language="C#" MasterPageFile="/MasterPages/BaseTemplate.Master" %><%@. Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %><%@. Import Namespace="System.Web.Services" %><%@. Import Namespace="Common" %><script runat="server"> </script><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderPage" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> <Scripts> <asp:ScriptReference Path="PageMethod.js"/> </Scripts> </asp:ScriptManager> <center> <table> <tr align="left"> <td>Write current date and time in session state:</td> <td> <input type="Button" onclick="SetSessionValue('SessionValue', 139135)" value="Write" /> </td> </tr> <tr align="left"> <td>Read current date and time from session state:</td> <td> <input type="Button" onclick="GetSessionValue('SessionValue')" value="Read" /> </td> </tr> </table> </center> <hr/> <span style="background-color:Aqua" id="ResultId"></span> </asp:Content>
 
 

PageMethods.js looks like this:

// PageMethods.jsvar displayElement;// Initializes global variables and session state.function pageLoad(){ displayElement = $get("ResultId"); PageMethods.SetSessionValue("SessionValue", Date(), OnSucceeded, OnFailed);}// Gets the session state value.function GetSessionValue(key) { PageMethods.GetSessionValue(key, OnSucceeded, OnFailed);}//Sets the session state value.function SetSessionValue(key, value) { PageMethods.SetSessionValue(key, value, OnSucceeded, OnFailed);}// Callback function invoked on successful // completion of the page method.function OnSucceeded(result, userContext, methodName) { if (methodName == "GetSessionValue") { displayElement.innerHTML = "Current session state value: " + result; }}// Callback function invoked on failure // of the page method.function OnFailed(error, userContext, methodName) { if(error !== null) { displayElement.innerHTML = "An error occurred: " + error.get_message(); }}if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
 
i'm getting the feeling it is simply a limitation in the current version of Ajax.Net to use these
methods in the code behind file - did someone pullled that off??
 
thanks

Hi,

Tried my code inside an Ajax project and got the same results.

here is my code (basically taken from the site examples)

<%@. Page Language="C#" MasterPageFile="/MasterPages/BaseTemplate.Master" %><%@. Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %><%@. Import Namespace="System.Web.Services" %><%@. Import Namespace="Common" %><script runat="server"> </script><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderPage" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> <Scripts> <asp:ScriptReference Path="PageMethod.js"/> </Scripts> </asp:ScriptManager> <center> <table> <tr align="left"> <td>Write current date and time in session state:</td> <td> <input type="Button" onclick="SetSessionValue('SessionValue', 139135)" value="Write" /> </td> </tr> <tr align="left"> <td>Read current date and time from session state:</td> <td> <input type="Button" onclick="GetSessionValue('SessionValue')" value="Read" /> </td> </tr> </table> </center> <hr/> <span style="background-color:Aqua" id="ResultId"></span> </asp:Content>
 
 

PageMethods.js looks like this:

// PageMethods.jsvar displayElement;// Initializes global variables and session state.function pageLoad(){ displayElement = $get("ResultId"); PageMethods.SetSessionValue("SessionValue", Date(), OnSucceeded, OnFailed);}// Gets the session state value.function GetSessionValue(key) { PageMethods.GetSessionValue(key, OnSucceeded, OnFailed);}//Sets the session state value.function SetSessionValue(key, value) { PageMethods.SetSessionValue(key, value, OnSucceeded, OnFailed);}// Callback function invoked on successful // completion of the page method.function OnSucceeded(result, userContext, methodName) { if (methodName == "GetSessionValue") { displayElement.innerHTML = "Current session state value: " + result; }}// Callback function invoked on failure // of the page method.function OnFailed(error, userContext, methodName) { if(error !== null) { displayElement.innerHTML = "An error occurred: " + error.get_message(); }}if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
 
i'm getting the feeling it is simply a limitation in the current version of Ajax.Net to use these
methods in the code behind file - did someone pullled that off??
 
thanks

Well, your code above you didn't include the relevant methods from the codefiel for us to evaluate, and in fact what you did copy looks a lot like a page that doesn't use a codefile (the @.import statements and the lack of the CodeFile= attribute in the @.Page directive being my first clues on that). Given that you're getting and setting session variables, however, do your C# methods int eh codefile have the EnableSession=true set on the webmethod attribute flags? that could be the problem as well.

that was the problem indeed!!! thanks!!Smile

Wednesday, March 21, 2012

CalendarExtender seems to cause loss of Session

I have a web app which passes a Session object between pages. When I added a CalendarExtender to the page, that Session object is null when I navigate to the next page, even if I never actually click on the associated ImageButton. The version number of the AjaxControlToolkit.dll is 1.0.10920, which I believe is the latest.

Any thoughts?

Thanks.

Dan

I noticed another post today about the CalendarExtender causing memory leaks in IE. Based on one of the suggestions in that thread, I tried my app in both IE7 & Firefox outside of VS. It exhibited the same problems as with IE inside VS.

Any suggestions would be greatly appreciated.

Thanks.

Dan


Hi Dhurwitz,

As we know, CalendarExtender works on the client and session works on the server side so it seems your issue is not caused by the CalendarExtender.

I suggest that we should use a discharging method. First remove the CalendarExtender from your page and have a test. Here is thedocument which shows how to resolve the session issues.

Best regards,

Jonathan