Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In an aspx page, I defined such as
ASP.NET
<asp:Button ID="Button2" runat="server" Text="Reset" BackColor="#990000" Font-Bold="True" ForeColor="White"  OnClientClick="basicPopup('page2.aspx');return false;" />
......
function basicPopup(s) {
var userName = document.getElementById('txtUser').value;  // txtUser.Text = "John"
'<%Session["User"] = "' + userName + '"; %>';
alert('<%=Session["User"] %>');     // alert also display "John" correctly
popupWindow = window.open(s, 'popUpWindow', 'height=200,width=300,left=100,top=30,resizable=No,scrollbars=No,toolbar=no,menubar=no,location=no,directories=no, status=No');
}

In page2aspx page, the Session variable's value displays differently
C#
txtUser.Text = (string)Session["User"];  // value:  ' + userName + '

How can this problem be solved? Thanks.
Posted
Comments
ZurdoDev 13-Mar-15 8:28am    
What do you mean?
s yu 13-Mar-15 10:13am    
After I set a session variable in page1, I retrieved the session variable's value on page2. However, in page 2, the session variable's value is incorrect. The value in page2 is ' + userName + '
ZurdoDev 13-Mar-15 10:30am    
Do it in C# code instead of trying to use scripting tags in the aspx page. You have the syntax wrong as Solution 1 pointed out.

1 solution

Your server code runs first so this is executed

Session["User"] = "' + userName + '";


which makes your session variable be "' + username + '". When your .net code has finished running it sends the generated html to the client, so view the source of your page and you'll see

function basicPopup(s) {
var userName = document.getElementById('txtUser').value;
'';
alert('' + userName + '');
popupWindow = window.open(s, 'popUpWindow', 'height=200,width=300,left=100,top=30,resizable=No,scrollbars=No,toolbar=no,menubar=no,location=no,directories=no, status=No');
}


The reason you are seeing "John" in the alert isn't because your session variable contains "John", it is because your session variable contains javascript that, when executed, shows the value of the username variable which works on that page as that variable is defined.

Basically you can't do what you're trying to do, you can run server code in your javascript. You should pass the data on the url and have page2.aspx read the value from the url and store it in the session

C#
function basicPopup(s) {
var userName = document.getElementById('txtUser').value;
s += "?user=" + encode(userName);


In your page2.aspx;

Session["User"] = Request.QueryString["user"];
 
Share this answer
 
v2
Comments
s yu 13-Mar-15 10:20am    
Thanks for your solution. But got error: Microsoft JScript runtime error: 'username' is undefined on the line of the code:
s += "?user=" + encode(username);
Any hint for the new problem? Thanks again.
F-ES Sitecore 13-Mar-15 10:28am    
I meant "userName" as js is case-sensitive. I've updated the original solution.
s yu 13-Mar-15 11:18am    
after changed to s += "?user=" + encode(userName);
still got the same error.
F-ES Sitecore 16-Mar-15 5:11am    
function basicPopup(s) {
var userName = document.getElementById('txtUser').value;
s += "?user=" + encodeURI(userName);
popupWindow = window.open(s, 'popUpWindow', 'height=200,width=300,left=100,top=30,resizable=No,scrollbars=No,toolbar=no,menubar=no,location=no,directories=no, status=No');
}

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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