0

I have a client global variable in javascript. I need to access this global variable from the server side which is living on different page. how can i achieve this without a query string? if session state required, then how can i set the session from the javascript?

EDITED

ok.. here is the full scenario.. the page itself is a sharepoint web page, i have only control on the web part. within the webpart i have a button that does window.open to open another page. Now how can i pass the javascript variable from the main page to the other page and make it accessible in the code-behind?

user384080
  • 4,576
  • 15
  • 64
  • 96

3 Answers3

0

Session is a server side data store. Try using cookies.

Prashanth Thurairatnam
  • 4,353
  • 2
  • 14
  • 17
0

I can think of a few ways to do it, but I'll recommand you this. The basic steps are:

  1. Use JavaScript to popup a modal dialog alone with the global variable you want to pass as a argument (not query string).

  2. On the popup page, use JavaScript to set the global variable to a hidden field and then initiate a postback to itself.

  3. At code behind, retrieve the global variable from hidden field and store it in session data.

  4. Now, the global variable is available in Session space.

Note: You'll need jQuery 1.7.1 or higher.

Step 1 code:

this is your popup link or button.

<a id="popAspxWeb" href="#" onclick="javascript:PopPage('ModalPopup.aspx','galbal_variable_data');">Click to Popup</a>

here's the javascript code to open popup window.

<script type="text/javascript">
    function PopPage(page, data) {
        var result = window.showModalDialog(
        page, // popup this page
        data, // with this data
        "dialogWidth:500px; dialogHeight:500px; resizable:no; status:no; center:yes");
    }
</script>

Step 2 code:

you'll need these. first one to store the data get passed in and second one to flag page postback or not.

<input id="hidData" runat="server" type="hidden" />
<input id="postback" runat="server" type="hidden" value="false" />

here's the javascript:

<script type="text/javascript">
    var data = ""; // global variable to store data from parent window.

    $(document).ready(function() { //jQuery code to be executed when document is fully loaded.
        args = window.dialogArguments; // obtain argument data and assign to global variable.
        if ($("#postback").val().toString() == "false") { // if first visit to page
            $("#hidData").val(data); // assign argument data from global variable to hidden field.
            $("#postback").val("true"); // flag the form is posted back.
            document.forms[0].submit(); // trigger form post.
        }
    });

    function ReturnAndClose() { // you can optionally return data back to the parent window.
        window.close(); // this closes modal pop up.
        window.returnValue = document.getElementById('return_data').value; // this returns a value to parent window.
    }

</script>

Step 3 code:

code behine to access galbol variable data.

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        string data = this.hidData.Value;
        this.Session["data"] = data;
    }
}
Ray Cheng
  • 12,230
  • 14
  • 74
  • 137
-1

No, Sessions can't be accessed by javascript directly. It's stored on server, while javascript is running on client. But can be done indirectly please refer to the answer on below link

Get Values from Previous Pages using different approaches

Check Point 4 in this above link.

I have explained: cross page postback, adding PostBackURL property of control

Hope it serves your purpose as per your edit.

Community
  • 1
  • 1
Murtaza
  • 3,045
  • 2
  • 25
  • 39
  • What bulls**t is this, if you have guts to down vote then comment and explain why is it done? – Murtaza Apr 27 '12 at 06:44
  • mate.. solution from you is setting session from server side.. read my question carfully.. i need to set it from client side! – user384080 Apr 27 '12 at 06:55
  • @user384080 - First line in my answer explains that we cannot set session from client side. Please read the answer carefully and then down vote. If you are not able to find and all answers says that we can not access sessions directly in javascript, then my post is correct and there is no need to down vote it. – Murtaza Apr 27 '12 at 08:01