user1342164
user1342164

Reputation: 1454

Grab a session value in Javascript?

I use the code line below in a javascript function to get a textbox value. Is there anyway to grab a session value instead. So like replace LastNameTextBox.ClientID with Session("LastName") etc.?

var LastNameTextBox = document.getElementById('<%= LastNameTextBox.ClientID %>');

Then I use the following to add it to a pdf.

   printWindow.document.write('Last Name:');

Upvotes: 0

Views: 87

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074385

It would be exactly the same but with Session("LastName") instead of LastNameTextBox.ClientID), e.g.:

printWindow.document.write('Last Name: <%= Session("LastName") %>');

...although you may want to make sure the name is escaped correctly, since names can contain ' characters (amongst other things), which if unescaped would cause an error in the script when it went to the client browser, because the browser would see:

printWindow.document.write('Last Name: O'Toole');
// Syntax error -------------------------^

If you're using .Net 3.5 or later, you can use JavaScriptSerializer to do the escaping for you.

Upvotes: 3

Related Questions