JavaScript does not reflect updates made to the ASP.Net session

After clicking the button, I trigger the JavaScript to retrieve the session information. However, I am encountering an issue where the value of the session is not being updated.

alert('<%= Session["file"]%>');

Answer №1

The information may not be current if it is altered after the webpage has loaded.

Consider exploring page methods (an ajax system) or another method involving ajax for real-time updates.

Answer №2

When inline code is displayed on a page, it remains static and does not change, which is the expected behavior. It is recommended to use a Hidden field instead.

Here is an example of how to mark-up:

<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

And the corresponding code-behind implementation:

protected void Button1_Click(object sender, EventArgs e)
{
    Session["file"] = "Data Here";
    HiddenField1.Value = Session["file"].ToString();
}

Additionally, here is a snippet of JavaScript that retrieves the value from the HiddenField:

alert(document.getElementById('<%= HiddenField1.ClientID %>').value);

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

What is the most effective method to include JSON data containing various IDs at the end within an $http.get() request in AngularJS?

I'm facing a challenge with displaying JSON items that have an array of different ids at the end of the URL (/api/messages/:messageId). For instance, accessing /api/messages/12345 would return {"subject":"subject12345","body":"body12345","id":"12345"} ...

Configuring IP Whitelisting for Firebase Cloud Functions with MongoDB Cluster

What is the process for including my Firebase Cloud Functions in the IP whitelist of my MongoDB cluster? Error Message: ...

Creating a Personalized Tab Layout with react-bootstrap

Incorporating react-bootstrap components and styled components, I have implemented tabs in my project. The current appearance of the tabs is as shown here: https://i.sstatic.net/rPnlO.png However, my requirement is to have the tabs look like this inst ...

Easily move a group of HTML elements at the same time with a simple

Exploring HTML5 Drag and Drop with Multiple Elements When it comes to dragging and dropping multiple elements in HTML5, there seems to be a limitation with the default draggable attribute. This attribute allows only one element to be dragged at a time, ma ...

Stop the default behavior of a link based on the Ajax response

A controller has been created in Magento to check if there are any products in a list. If products exist in the list, it will return true; otherwise, it will return false. The front-end below triggers an ajax call. It must remain a link and cannot be chan ...

Navigating through a series of items in VueJS can be easily accomplished by using a

Below is the Vue instance I have created: new Vue({ el: '#app', data: { showPerson: true, persons: [ {id: 1, name: 'Alice'}, {id: 2, name: 'Barbara'}, {id: 3, name: &a ...

Pass a JSON object containing an additional parameter to the controller in a Spring MVC application

1.- My goal is to receive two parameters in a specific format in my controller: */Controller @RequestMapping(value = "/insertCatalogGeneric", method = RequestMethod.POST) public @ResponseBody String insertCatalogGeneric(@RequestBody CatalogGeneri ...

End node.js once and for all

After starting my server using forever start start.js It ran normally. However, when I attempted to stop it with forever stopall The server was removed from forever list as expected Nevertheless, upon running lsof -i tcp:3000, my server still showed u ...

Undefined output in Typescript recursion function

When working with the recursion function in TypeScript/JavaScript, I have encountered a tricky situation involving the 'this' context. Even though I attempted to use arrow functions to avoid context changes, I found that it still did not work as ...

Issue with Vue.js devtool not appearing in browser

Currently, I am integrating moment.js into a Vue component but I am facing an issue where certain changes are not being reflected in vue devtools. Here is an example of my code: export default { data() { return { moment: moment(), ...

Exploring SQL methods within WebMatrix using ASP.NET Razor

Could there be any issues with the variable query4 because the application doesn't display any messages, but it fails to insert the data into the specified table? @{ var userId = Request["UserId"]; var Type = Request["type"]; var db = Dat ...

Utilizing Highcharts to create visually engaging data plots within a Flask-powered web application

I am trying to fetch data from a MySQL server and display it on a web server using the Highcharts JavaScript library for plotting. I have successfully retrieved the data from the MySQL database and sent it from the server-side to the client-side as JSON da ...

Retrieving text content from a file using React

I've been experiencing difficulties with my fetch function and its usage. Although I can retrieve data from the data state, it is returning a full string instead of an array that I can map. After spending a few hours tinkering with it, I just can&apos ...

JavaScript quiz featuring randomized questions with selectable radio buttons

I need assistance in creating a feature on my webpage where users can select the number of questions they want to answer (ranging from 1 to 10). The selected number of questions should then be displayed randomly, without any duplicates. I am not very famil ...

What is the best way to showcase a PDF in Django that is saved as a blob in a MySQL database?

Within my web application, users have the ability to upload a PDF file which is then stored directly in the MySQL database for security reasons. Utilizing the code snippet below, this process allows the uploaded file to be safely saved within the database ...

What is the best method for sending form data, specifically uploaded images, in Python Bottle with Ajax?

This form belongs to me <form method='post' enctype='multipart/form-data' id='uploadForm' name='formn'> <input type='file' value='' name='newfile'> <input type=&a ...

What causes compatibility issues between JEST and import statements in NEXTJS?

Just starting out with unit testing in JavaScript and I'm attempting to create a unit test for a Next JS project. However, when running the test, I encountered the following error: Code: import {isBase64} from '../../service/base64-service&a ...

Tips for inputting information without redundancy after selecting a specific designation

I'm experiencing a challenge with this code as it repeats when already chosen. My goal is to add data in the database once something has been selected. When I click on spin, the randomizer will choose a name and update their status to "done". Subsequ ...

Why does my form keep causing a postback even though I'm trying to call it asynchronously?

Currently, I am working on a simple ASP.NET webpage that contains a few fields. When the user clicks on the submit button, I am making an asynchronous call to post the data. However, there is an issue where the entire page is posting back, and I am unable ...

Mocha struggles to locate the ID within an input field - react-native

I am attempting to retrieve the text of an input using the .text() method in Selenium. However, every time I try to locate the element, it says that the specified ID was not found. (I am working with the Input component from NativeBase, but I have also te ...