Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi......

I want to get and set application and session varible values in javascript.I am able get varible value.I got a soln on one website for setting value of application and session variables.when i am used it got an error.Is there any other way.
My code is
//get variable
var userId = '<%=Application("userId")%>';

//set varible

XML
var myvar = "Hey Buddy";
'<%Application["userId"] = "' + myvar +'"; %>' ;



while set varible value i got an error:
BC30545: Property access must assign to the property or use its value.


if i use the following code
XML
var myvar = "Hey Buddy";
'<%Application("userId") = "' + myvar +'"; %>' ;
alert('<%=Application("userId")%>');


i got an error Character is not valid

Provide a soln.

[Edit]Tag added[/Edit]
Posted
Updated 20-Oct-12 2:32am
v2

1. take a ScriptManager
2. set it property EnablePageMethods as true
4. write a public static method and mark it attribute as [WebMethod]
5. call above static method from javascript.
C#
[System.Web.Services.WebMethod]
    public static void ChangeSession(string value)
    {
        HttpContext.Current.Session["yourSessionKey"] = value;
    }

in javascript

HTML
<input type="button" value="change session" onclick="ChangeSession('yourNewSessionValue');" />
 
Share this answer
 
Session & Application Are Server Object Which Creates / Modifies & Deletes On Server Only. You Can Get Them In JavaScript Like This :-

var name = '<%= Session["MyName"] %>';

But You Can Not Set Them Because Javascript Runs On Client-End And Can't Effect Server Variable(s).

If U Want To Do This You Should To Create A "XmlHttpRequest" Requset Who Can Send Your Request On Server Via JavaScript And U Can Creates / Modify & Delete Server Variable(s) On Server.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900