Extract data from ASP.NET session to JavaScript

I have been developing a web app using asp.net and angularjs. In order to pass my session variable from c# to javascript, I have implemented the following code:

<script type="text/javascript">
    var pm = "<%= Convert.ToString(Session["mysession"]) %>";
    var log = "<%= Convert.ToString(Session["radio"]) %>";        
</script>

However, I encountered an error: BC30203: Identifier expected.

This error specifically occurred on:

Line 111: var pm = "<%= Convert.ToString(Session["mysession"]) %>";

Can anyone provide insights into what the potential reason for this error could be?

Answer №1

It is highly probable that the value of Session["mysession"] includes a quotation mark, which may be causing your code to break due to invalid syntax.

If you are certain that there are no single quotes in the session value, you can use the alternative method shown below. Otherwise, make sure to escape the quotation mark when assigning a value to Session["mysession"] on the server side.

var pm = '<%= Convert.ToString(Session["mysession"]) %>';

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using two modal popups while passing an identifier

UPDATE: In my investigation, I discovered that a plain input tag without MVC RAZOR works as expected: <input type="text" class="hiddenid2" /> //WORKED However, when using the following code, it does not work: @Html.Editor("id", "", new { htmlAtt ...

"Executing the command 'npm run dev' is successful, however, the command 'next dev' does not yield the expected result

Trying out Next for the first time using npx create-next-app, but running into issues with the scripts. While npm run dev works without any problems, executing next dev gives me an error saying zsh: command not found: next. Any idea why this is happening? ...

Vue.js and Firestore will only update variables that do not have empty string values

Modify the variables that are not empty strings const state = reactive({ birthNumber: '', phoneNumber: '', DoctorName: '', DoctorPhone: '', }) db.collection(state.user.uid).doc( ...

What is the most reliable way to verify when knockout has completed data binding?

Implementing the panelBar feature from KendoUI into an app at work has been a challenge. It seems that KendoUI and KnockOut don't play nicely together. The issue I am facing is that the panelBar implementation is being disrupted by a dynamic knockout ...

What is the best way to incorporate a third-party element into Vue using a script tag?

I am in the process of developing a website and I would like to include a widget that links to a podcast on BuzzSprout. Initially, I created the site using HTML to test out different designs, but now I am looking to transition it to VueJS. In my HTML vers ...

Issues with communication between Android and the Webservice are causing values to not be passed

Hey there! I've got a web service set up like this: [WebMethod] public bool SyncGeneralDataToServer(string macaddress, string userid, string password, string computername, string ...

Refreshing Page Following Ajax Submission

Currently, I am working on a Single Page Application using asp.net MVC. When I make a post request to the server using Ajax, the Web API performs parameter validation and then returns a Json String. The code snippet for the Web API function is shown below ...

Issue encountered while replacing webapp.dll in IIS7: Compilation Error

There seems to be an issue when I try to overwrite my asp.net .dll on the web server. The server is using Windows 2008 and IIS7, and the only solution I have found so far is to restart IIS7 completely. Error Message Description: A compilation error occurr ...

What is the best way to test a try/catch block within a useEffect hook?

Hey, I'm currently dealing with the following code snippet: useEffect(() => { try { if (prop1 && prop2) { callThisFunction() } else { callThatFunction() } ...

Encountering a FeathersJS Twitch OAuth 401 Unauthorized error

I'm a newcomer to FeathersJS and I've been trying to set up OAuth login with Twitch. Following the steps outlined in the Feathers documentation for setting up GitHub login OAuth, I created a Twitch OAuth application. However, when attempting to s ...

bxSlider adds in an empty slide after deleting an image

Whenever I click a link in my navigation, I want to remove certain images from the page. I tried implementing a solution, but now I have two empty spaces where the images used to be. How can I remove those as well? I searched on Stack Overflow for a soluti ...

trim() function acting strangely

There seems to be an unexpected occurrence with the trim() function, as it is removing the á character. https://i.stack.imgur.com/whZBN.png This particular code snippet is typically used in various JavaScript projects without any issues. However, a clie ...

Having trouble with Wookmark not working in Jquery UI?

Hey there, just wanted to share that I'm currently utilizing the Wookmark jQuery plugin $.post('get_category',data={type:'All'},function(data) { $.each(data,function(item,i){ var image_ ...

What is the best way to iterate over JSON data and organize the output based on the key value?

Let's say I want to filter through the JSON data below and only push the home_team_conference if it matches Southeastern. My goal is to organize the data by home_team_conference in order to display each array on different pages of my website. Currentl ...

The appearance of SVG markers varies depending on the screen they are viewed on

Latest Update: I have finally figured out the screen issue. The device pixel ratio is to blame. Icons appear smaller on devices with lower window.devicePixelRatio. One solution I found is to adjust the icon size based on window.devicePixelRatio, like this ...

Developing with AngularJS, Node, and socket.io on a local server

I am currently working on an app using AngularJs, node, and socket.io to enable shared data across different clients. I found inspiration from this example: https://github.com/mhevery/angular-node-socketio After setting everything up, it functions perfect ...

An analysis of Universal Angular.io and Prerender.io from the viewpoint of Googlebot

Currently, my website is set up with Angular 1.4.x and prerender.io, which delivers static cached pages to Googlebot. Googlebot visits each page twice - once by hitting the URL directly, and then again by appending ?_escaped_fragment_ to the URL to access ...

Switch out the HTML content within the JavaScript include

Here is the situation: <script type="text/javascript" src="http://xy.com/something.js"></script> This code snippet in my PHP/HTML file loads the entire something.js file. Inside that file, there are lines containing "document.write..." which ...

Is it feasible to access an Exchange server in C# without using a domain name?

I am currently developing a web application to access an Exchange Server 2010 in order to retrieve emails from the server. My question is, we do not have a domain name for this server. All I have is its IP address. Is it possible to utilize Exchange Web Se ...

problem encountered when inserting data (GET method) in a loop using window.location

I'm currently utilizing sailsjs to extract data from the GET parameter within the URL (mydomain.com/prod_master/addsupp). The specific page is /prod_master/addsupp, designed to receive GET parameters for database insertion. In my Javascript code, I ...