0

Is there a way to pass values from client side to server-side, server side process this value, then return it to client side without reload or postback?

I am using ajax to pass the value to server side. Here is my client side code:

var baseUrl = '<%= ResolveUrl("~/") %>';
$.ajax({
    type: "POST",
    url: baseUrl + "Pages/TeamTimeTest/teamtime.aspx/setjson",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({
        strpath: $("#hdnfld").val()

    }),
    success: function (data) {
    alert(data);
    },
    error: function (response) {
        console.log(response);
    }
});

Here is my code behind code:

[WebMethod(EnableSession = true)]
public static string setjson(string strpath)
{
    HttpContext.Current.Session["strDwnPath"] = strpath;
    var val = HttpContext.Current.Session["strDwnPath"].ToString();
    return val;
}

It returns undefined for me. But when I reload it gives me the correct value. Is there a chance that I could have that value even without reloading?

  • 1. Don't you mean `ToString()` (upper T)? 2. You do realise that you're basically setting a variable and then returning that variable. The server side code makes no sense. – Maria Ines Parnisari Nov 17 '15 at 02:31
  • edited. yes this was just a sample. when I pass this value to the session, initially the session does not hold that value. but when I reload the page, the session now holds that value. what I would want is to have that value without reloading or postback – Voltaire John Secondes Biton Nov 17 '15 at 02:35
  • you modify the session and return another value, so at front page the session didn't change. – MichaelMao Nov 17 '15 at 02:40
  • Are you able to debug and put a breakpoint on the setjson method? Does your request reach the method call? – TejSoft Nov 17 '15 at 02:43
  • yes. it reaches the method call. the session will only hold that value that i pass when i reload the page. But i dont want to reload or postback because i am dealing with a large amount of data. – Voltaire John Secondes Biton Nov 17 '15 at 02:48
  • 1
    It looks like accessing session problem via ajax calls. Check this solution: http://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api – TejSoft Nov 17 '15 at 02:55
  • Actually I can have access to any sessions on that method. But it seems when I assign a value to a session inside a page method, the session will not hold that value unless if I reload it. Maybe that is how it really works? I can only get the data in page method but cannot set and get it at the same time? – Voltaire John Secondes Biton Nov 17 '15 at 03:14

1 Answers1

0
HttpContext.Current.Session["strDwnPath"] = strpath;
var val = HttpContext.Current.Session["strDwnPath"].ToString();
return val;

I should have done this instead.

var val = strpath;
return val;

All i need here is to get the returned value and it did without reload.