retrieving session variables from the server side in javascript

I have set a session variable in the backend (code-behind ascx.cs page) and now I need to access that same value in a checkbox checked event using JavaScript.

Below is my JavaScript function code:

 $(document).ready(function () {
    $('#<%= gvPRCertInfo.ClientID %> input[type="checkbox"]').change(function () {

        var background1 = null;
        background1 = '<%= Session["FriendlyData"] %>';
        alert(background1); // The value returned is 'system.data.dataset'
        if ($(this).is(':checked')) {
            var signValue = 
     $(this).closest('tr').children('td:eq(4)').html();

        }
    });
 });

The variable <%= Session["FriendlyData"] %> contains a dataset with values assigned in the backend. However, when I try to access it in the JavaScript function above, the alert shows 'System.data.dataset' instead of the actual session value.

If anyone can provide guidance on how to correctly retrieve the dataset value in the JavaScript function, it would be greatly appreciated.

Thank you in advance!

Answer №1

In order to properly handle your codeback object, you must convert it into a string format. One way to achieve this is shown below:

// Retrieving dataset in .aspx.cs file
DataSet ds = GetMyDataSet();
Session["dataset"] = ds;

// Accessing dataset in .aspx file
<script>
var ds = '<%=((DataSet)Session["dataset"]).GetXml()%>';
</script>

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

Unable to locate any division element by using document.getElementById

I'm encountering an unusual issue in my code. I am attempting to change the background color of certain divs using Javascript, but for some reason, when I use "document.getElementById", it is unable to find the div and returns NULL. Here is the probl ...

What sorcery transforms an integer + string into a string?

Recently, I discovered an unconventional method for implementing ToString() and now I'm curious about its workings: public string tostr(int n) { string s = ""; foreach (char c in n-- + "") { //<------HOW IS THIS POSSIBLE ? s = s ...

Clicking again on the second onclick attribute

I have an image file named image1.png. When the button is clicked for the first time, I want it to change to image2.png. Then, when the button is clicked for a second time, I want it to change to yet another image, image3.png. So far, I've successful ...

Is it no longer possible to create custom linked Context Menus in Tabulator 5?

After using Tabulator for a few years, we have decided to switch over to Angular v13 and upgrade to the new Tabulator 5.x. In our previous implementation, we had set up a custom ContextMenu in the Table Column Definition like this: contextMenu: this.TableR ...

Retrieving parameters from a class that is handed over from the constructor to a function by leveraging the rest parameter feature

I am encountering an issue where the Method is not reading the values in the arguments, despite my attempts to pass each argument through the constructor into the method for encryption. Could someone please point out what I might be doing wrong? class A ...

Is there a way to animate without specifying a duration?

Exploring the capabilities of the Animated component in react-native, I came across the powerful Animated.timing(); function which operates within a specific duration defined as duration: 2000,. While duration is useful in certain scenarios, I found myself ...

Error: An error occurred because the program attempted to read properties of a null value, specifically the property "defaultPrevented"

There seems to be an issue with a script error involving both the bootstrap alert and my own close alert tag. I have a script set up to automatically close alerts after 2.5 seconds when certain user actions trigger them, such as creating a blog post or del ...

Navigating with Reach Router only updates the URL, not the component being rendered

Is there a way to programmatically navigate using Reach Router in React? I have noticed that when updating the URL, the route does not render. Even though the URL changes, the original component remains displayed according to the React developer tools. Ho ...

Issue with date comparison operator logic

Here's a simple JavaScript function I have: function checkTime() { var current = new Date(); var target = new Date('April 10, 2017 12:11:00'); if (current < target) { $('#modalnew').modal('show'); } els ...

What should be done when dealing with ScriptManager AddHistoryPoint and no parameters are provided?

Using a ScriptManager with EnableHistory set to "True", I employ the AddHistoryPoint method to store filter terms for a specific filter on a page. For instance: this.ScriptManager.AddHistoryPoint("filterterm", "somevalue"); This results in the address b ...

Steps to Remove the Displayed Image upon Click

I have a collection of images such as {A:[img1,img2], B:[img1]}. My goal is to remove the array values that correspond to previewed images upon clicking the delete button. Each image is equipped with its own delete button for this purpose. This snippet ...

What are the signs of a syntax error in a jQuery event like the one shown below?

One of my forms has an ID attribute of id ='login-form' $('#login-form').submit(function(evt) { $('#login-button').addClass('disabled').val('Please wait...'); evt.preventDefault(); var postData = ...

The fragment shader must not declare any varyings with the same name but different type, or statically used varyings without being declared in the vertex shader. An example of this is

I'm struggling with shaders because I want the fog to be reflected in the water using Three.js sky_sun_shader. I added the following code snippet to the fragment Shader: THREE.ShaderChunk[ "fog_pars_fragment" ], THREE.ShaderChunk ["fog_fragment" ], ...

Developing a SQL Server table dynamically using C#

Is there a way to generate a SQL Server table using code? Take a look at the code I'm working with: using (SqlConnection con = new SqlConnection(conStr)) { try { // // Open the SqlConnection. // con.Open(); ...

Assigning the window.location in the code behind to a LinkButton's onClientClick event

Is it really this difficult? I am trying to change window.location onclientclick of a linkbutton and do this from the code behind. lb.OnClientClick = "window.location = 'Contact.aspx'"; It's not working, just refreshes the current page. l ...

Counting the number of times a form is submitted using a submit button and storing the data

After researching for a few hours, I've gathered information on different techniques for storing data related to a submit button counter in a text file. <form action="/enquiry.php" method="post" name="form"> <label>Name *</label> &l ...

Webpack: Live reloading is not functioning properly, however the changes are still successfully compiling

Could someone help me understand why my React application, set up with Webpack hot reload, is not functioning properly? Below is the content of my webpack.config.js: const path = require('path'); module.exports = { mode: 'development&apo ...

The parameters used in the json.parse function in javascript may be difficult to interpret

Currently, I am examining a JavaScript file on this website. It contains the following code: let source = fs.readFileSync("contracts.json"); let contracts = JSON.parse(source)["contracts"]; I'm curious about what exactly the JSON.parse function is d ...

AngularJS service for exchanging information among controllers

I am working on an AngularJS application (1.4.10) where I need to share data between two controllers. To achieve this, I created a factory: .factory('CardsForService', function($http, URL){ var service = { "block_id": '', ...

Having difficulty refreshing metadata on the client side within Next.js through client-side data retrieval

Having some trouble with updating metadata using NextSeo when fetching data on the client side. useEffect(()=>{ fetch(assetURL) .then((res) => res.json()) .then((data) => { console.log(data); }) .catch((error) => { console.err ...