views:

94

answers:

4

Hi guys,

I'm trying to pass a variable stored in the Session object of my ASP.NET page with jQuery, representing the progress of a process running on the server as a percentage.

i.e:

Session['progress'] = 37;

I've looked through some of the answers here; my code uses the same method as a question aswered correctly here:

<head>
...
    <script type="text/javascript">  
        function updateProgress() {
             var progress = '<%Session["progress"].ToString();%>';
        } 
    </script>
...
</head>

The ASP.NET compiler is returning a NullPointerException. I'm unsure if this is because the Session object hasn't been instantiated yet (as this is occurring at compile time); or perhaps the Session object isn't receiving the variable properly (if this is the case, how can I go about checking this out?)

Any ideas?

I need to find some way off passing this value from the server to the client so a jQuery progress bar may be updated for the user.

Thanks!

A: 

Use

<%=Session["progress"].ToString()%>

to output data to html page

SageNS
I've done this already, but I get a NullPointerException when the page compiles?
icecreamsoop
+2  A: 

I think you are misunderstanding the order in which things occur. While SageNS found your error, he did not solve your problem.

The code you have simply writes a number to the javascript code, and the user sees:

<head>
...
    <script type="text/javascript">  
        function updateProgress() {
             var progress = '37';
        } 
    </script>
...
</head>

Successive calls to updateProgress() will always return 37.

You need to use jQuery's .ajax() function to make an asynchronous call to the server if you want to periodically update the server's progress.

Stargazer712
An Ajax call can only be made to a static webmethod though, and this cannot access any data stored in a non-static object.I need to access data stored in an object already existing via a static method - this is what I'm not too sure about.
icecreamsoop
+3  A: 

If you want to get a real-time changing number from a session variable in your page using jQuery, you'll need to make some type of ajax request. To access a value in the session state, you can write a function [WebMethod] to send gets/send the data back to your jQuery request.

jQuery Request :

jQuery.ajax({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: '{}',
    dataType: 'json',
    url: 'Default .aspx/TestMethod',
    success: function(result){
        alert(result.d);
    }
});

Server Page WebMethod :

using System;
using System.Web.Services;

namespace JqueryAjaxText
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string TestMethod()
        {
            return Session["progress"].ToString();
        }
    }
}
Zachary
I've tried this approach already; I received the following error:"CS0120: An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'"
icecreamsoop
+1  A: 

You can build off Zachary's answer and take it a step further by creating a WCF service and switching your $.ajax() call to be a GET instead of a POST. It won't be as large of a request (not that the POST was that big), but you'll also be using the proper HTTP verb. (POST is used to insert; GET should be used to retrieve data).

Your jQuery call could look like this:

jQuery.ajax({ 
    type: 'GET', 
    dataType: 'json', 
    url: 'MyService.svc/GetServerProgress', 
    success: function(result){ 
        alert(result.d); 
});

You would then create a WCF Service and create a service method named GetServerProgress.

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetServerProgress")]
    public int GetServerProgress()
    {
        return 37;
    }

Lastly, you'll probably want the client to have some JavaScript that calls setTimeout() to your $.ajax() method to that you periodically retrieve the server's progress from the WCF service.

Good luck!!

David Hoerster