0

I have to process an operation in Javascript using data stored in session (System.Web.HttpContext.Current.Session["Filtre"])

Is it even possible to catch and do operation in Javascript using the asp.net session ?

I already tried some sample like this one without success :

var f = '<%=Session["Filtre"]%>';

In the case where this is impossible (for security issues I guess), is it possible to call a aspx.cs function in javascript who will perform the operation ?

Have a good day.

2
  • Why don't you store your information during Page_Load into a variable, and then access this variable in your page? Commented Jul 2, 2015 at 14:18
  • Because my project use iframe, and my component .ascx isn't reload at every page .. Commented Jul 2, 2015 at 14:20

3 Answers 3

0

You cannot mix client side javascript and server side code (such as sessions). The processes do not run at the same time.

Your code

var f = '<%=Session["Filtre"]%>';

could work, BUT:

  • First the server needs to execute the <% %> block to generate a string, which is placed in some text
  • That text is sent to the browser, possibly as part of a page
  • Only in the browser it is interpreted and executed as javascript

There is no easy way for the browser to execute random server side code. Complicated ways use AJAX calls to call specifically designed methods on the server (instead of arbitrary code).

Sign up to request clarification or add additional context in comments.

Comments

0

using <scriptmanager> you can do

Please go through the below article

http://www.codeproject.com/Articles/525364/AJAX-for-Beginners-Part-3-Calling-Server-Side-Meth

Comments

0

Yes, your JavaScript can call a C# method if you annotate the method with WebMethod. This makes the method callable from remote Web clients (i.e. it becomes a 'page method'). MSDN.

You would need the EnableSession property set to true in order to use the Session. Example:

[WebMethod(EnableSession=true)]
public static int Example() {
...

Then call the page method from JavaScript, probably using jQuery.

   $.ajax({
        type: "POST",
        url: "MyPage.aspx/Example",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{ }",
        error: function (XMLHttpRequest, textStatus, errorThrown) { alert(langError + " " + textStatus); },
        success: function (msg) {
            alert(msg.d);
        }
    });

1 Comment

I'm not sure if the EnableSession attribute is actually needed for page methods (vs web methods) and don't currently have access to my development environment, but you can try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.