Combining the power of JavaScript with ASP.NET

My goal is to create a link that will change a session variable upon onclick event, like so:


<a href="/Index" onclick="<% HttpContext.Current.Session["location"] = location;%>" >
    <%=location%>
</a>

However, as the page processes, the session variable changes automatically while generating each anchor element with the onclick attribute. I attempted to solve this by creating a JavaScript function:


<script type="text/javascript" >
    function session(location) {
        HttpContext.Current.Session["location"] = location;
    }
</script>

Unfortunately, my attempt doesn't seem to work. Any suggestions or ideas would be greatly appreciated. Thank you in advance.

Answer №1

Take a deep dive into this discussion: Exploring the Relationship Between ASP.NET and Javascript.

Answer №2

Session refers to an ASP.NET server object that cannot be directly accessed by clients through JavaScript (although it may be stored as a cookie on the client, it is not intended to be accessed from the client-side). There are various ways to achieve what you need -

  1. One option is to store a value in a hidden input when a link is clicked, then retrieve that value on the server-side and store it in Session.

  2. An alternative approach is to store in Session during postback in a Click event handler for the anchor. One method could involve using the asp:HtmlAnchor control and setting up an event handler for the OnServerClick event.

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

AngularJS Reload Scenario: A new way to refresh and enhance

My current project involves the development of a mobile app where I am retrieving data from a REST GET call. Everything is running smoothly. However, I would greatly appreciate expert advice on how to seamlessly re-render this data if the user decides to ...

Node.js cannot locate the require function, resulting in a ReferenceError at node:internal/main/run_main_module:23:47

Currently, I am diving into the world of Node.js (Node.js v18.12.1) and embarking on my first server-building journey for a backend application. Below is the snippet of the initial code I have put together: const express = require("express"); const app = ...

Retrieve the number of days, hours, and minutes from a given

Is it possible to use JavaScript (jQuery) to calculate the number of days, hours, and minutes left before a specific future date? For example, if I have a timestamp of 1457136000000, how can I determine the amount of time remaining in terms of days, hours ...

What causes a never-ending loading cycle on a website when a 404 error occurs?

My Express.js/Node.js website is hosted on Heroku. I am facing an issue where the server does not properly send a 404 error when a file cannot be found. Instead, the page keeps loading endlessly. How can I make the server stop loading when it encounters a ...

Looking for a method to incorporate an onclick function into a column for renderCell within the MUI Datagrid. Any suggestions?

I'm currently developing a react application and utilizing the MUI Datagrid to present some data. I have incorporated a rendercell to insert an edit icon which should trigger a pop-up modal when clicked. Below is the column setup. export const specifi ...

Troubleshooting a simple test issue with React AXIOS post in an Express environment

I have successfully created a React front end and used a basic boilerplate to connect it with Express. However, I am now faced with the challenge of setting up a simple RESTful API, and the myriad of options available is overwhelming. My goal is to establi ...

Is there a way to deactivate a button upon clicking and then substitute it with a new button that serves a different purpose in AngularJS?

Is there a way to deactivate a button once clicked and substitute it with another button that performs a different task using AngularJS? Here is the button in question: <button type="submit" class="btn btn-small btn-default" ng-disabled="isteam==0 || ...

The publish-subscribe feature appears to be ineffective

Recently starting with meteor, I learned about the importance of removing autopublish. So, I decided to publish and subscribe to a collection in order to retrieve two different sets of values. Here is the code on my meteor side: Meteor.publish('chann ...

Using an array to substitute a string in JavaScript

xyz1 xyz2 xyz3 xyz4 xyz5 xyz6 xyz7 xyz8 xyz9 Given the CSV above, transform the sentence below by removing the double quotes and replacing them with the corresponding values from the CSV: "" is going with "" to "" for something to know. ...

By default, the function is triggered automatically upon clicking the button

I am encountering an issue with the code provided below. The testFunction is being executed automatically when the button click event occurs (default behavior of the CMS I'm using). However, I would like to override this behavior and prevent the testF ...

Using EffectComposer in conjunction with the alpha channel in three.js

Here's the code I'm working with: renderTargetParametersRGBA = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat,stencilBuffer: true }; colorTarget = new THREE.WebGLRenderTarget( SCALE * SCREEN_WIDTH, ...

Does the PHP include function act as an HTTP request?

After coming across an article highlighting the negative impact of using excessive HTTP requests on server speed, I started wondering about the performance implications of PHP includes. Specifically, I'm curious if sending 4 AJAX requests compared to ...

I can't figure out why the callback function for my $.get() request is not being triggered when I send data to the Express

Could anyone help me understand why my callback function is not being called properly? $.get("http://localhost:8080/", msg, function(data) { alert("Data" + data); }); It seems like the server receives the reque ...

creating curved lines in three.js

I'm looking for assistance in creating a globe using three.js where I can project arcs representing exports and imports between countries. I have a basic globe set up, but I need guidance on the following: 1. How to use country shape files instead of ...

Rotating an object in Three.js: Animate back and forth along two different azimuth angles

My three.js project features a 3D object that is meant to be viewed from the front only, as it appears as a single plane and is transparent from the back... Using orbitControls, I have restricted movement of both azimuth and polar angle... To enhance the ...

Guide to converting raw Mysql fields into objects using Node.js

I have written a code to retrieve all the rows from the article table in MySQL, but I would like to represent this data in object and array format so that I can send it to endpoints. app.get('/article' , function(req , res){ var connec ...

Generate random images and text using a script that pulls content from an array

My goal is to have my website refresh with a random piece of text and image from an array when a button is clicked. I have successfully implemented the text generation part, but I am unsure how to incorporate images. Here is the current script for the text ...

Utilizing Moment.js fromnow functionality with Vue.js and Laravel framework

Currently utilizing Laravel with Vue for my project Vue.http.headers.common['X-CSRF-TOKEN'] = $("#token").attr("value"); new Vue({ el: '#notificationMenu', data: { patients: [] }, ...

What is the most effective method for organizing an object by its values using multiple keys?

I'm interested in utilizing the sort method mentioned in the link but I am facing { "CYLINDER": 2986, "HYDRAULIC": 1421, "JACKS": 84, "INSTALLATION": 119, "REAR": 61, "JACK": 334, "TUBE": 1, "FEED": 114, "ASSEMBLY": 326, "DCS": 2 ...

What is the best way to incorporate external JSON data into Jade for rendering from Express?

As someone new to Node / Express, I am interested in using Jade and Express to serve my static files. In the past, I have successfully used Jade with gulp-jade and gulp-data to render Jade files by utilizing JSON files with gulp-data for each page. Combi ...