Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting value of session variable in javascript

Status
Not open for further replies.

sarfraz909

Programmer
May 26, 2010
2
0
0
NL
Hello everyone,

I have got a textbox (txtTemp) on my page. I want to put this textbox value in a session variable in javascript

I have tried the following in javascript:
function SetSessionVariable()
{
var temp = document.getElementById('<%=txtTemp.ClientID%>').value;
<%Session["TempVar"]="+ temp +";%>;
}

After this, I put the session variable value back in txtTemp in code like following:
this.txtTemp.Text = (string)System.Web.HttpContext.Current.Session["TempVar"];

But the textbox shows: + temp +
instead of the value in the temp variable.




Thanks...
 
Thanks for your reply...

But I am able to access it successfully...
For example when I write the following in javascript
<%Session["SessionVar"]="varTemp";%>;

I do get varTemp in my txtTemp text in code behind using

this.txtTemp.Text = (string)System.Web.HttpContext.Current.Session["SessionVar"];

However the only difference is that I want the value of varTemp instead of the text varTemp...

May be I am not putting the quotes at the right places in the above statement...

Regards...
 
No it can't be working successfully.

I just tried some quick example (realise you used some code-behind but this demonstrates the effect):
Code:
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function setVar(){
            alert("Testing");
            <% Session("TempVar") = "setVar Test" %>
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="txtTest" name="txtTest" value="<% response.write(Session("TempVar")) %>" />
        <button onclick="setVar();">Set Var</button>
    </div>
    </form>
</body>
</html>
Immediately when the page loads the value in the textbox is "setVar Test".

This is showing that the ASP code you stick in the page is executed as it is encountered regardless of where you place it in your html/javascript. The ASP engine doesn't pay any attention anything other than it's own tags.

If I did a view source on the page this is what I saw:
Code:
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head><title>
	Untitled Page
</title>
    <script type="text/javascript">
        function setVar(){
            alert("Testing");
            
        }
    </script>
</head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNDkzNTIwMjE5ZGTZuvnOdsz0MzaLoUbznGzHNOL2EA==" />
</div>

    <div>
        <input type="text" id="txtTest" name="txtTest" value="setVar Test" />
        <button onclick="setVar();">Set Var</button>
    </div>
    </form>
</body>
</html>

As you can see the ASP tag for setting the session variable is completely gone.

In order to do what you want you will need to use AJAX to send the new value back to the server, and then on the completion of that AJAX call set the value anywhere else you need it set client-side.

You might get better results for you question over in the ASP forum - as that is really what you are dealing with.

Hope that helps.
 
the reason the value varTemp is in session is because the code executed on the server when the template was rendered. this code did not execute on the client. to get a value from the client back to the server you need to submit a request. whether a full page submit or an ajax call. once on the server you can save the value to session.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top