`What is the best method for transferring values from JavaScript to C# during page load?`

My approach to solving this issue has been as follows:

  1. I added a hidden field on the aspx page named hdnTime.

  2. Next, I wrote a JavaScript function to assign a value to the hidden field.

    <script type='text/javascript'>
         function getLocalTimeZone() {
             document.getElementById('ctl00_bcr_hdnTime').value = 10;
             var hidden = document.getElementById('ctl00_bcr_hdnTime');
             var timezone = String(new Date());             
             hidden.value = timezone.substring(timezone.lastIndexOf('(') + 1).replace(')', '').trim();             
         }
    

  3. On the C# page load, I implemented the following code.

    ScriptManager.RegisterStartupScript(Page, GetType(), "disp_confirm", "getLocalTimeZone()", true);
    
    string test = hdnTime.Value;
    

However, I am encountering an issue where the value for hdnTime is blank.

If you have any insights or suggestions on what might be missing here, please let me know. Thank you!

Answer №1

In programming, the Page_Load event is triggered prior to the document.ready function. This means that any values set in document.ready will not be accessible within the Page_Load event.

One workaround is to assign a value upon the click of a button, and then retrieve that value using C#.

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

Versioning resources in Spring MVC with Thymeleaf

https://i.sstatic.net/ArllV.png Struggling with resource versioning in Spring Mvc 4 while using thymeleaf template engine. Despite the code provided, I am unable to see the new version URL when viewing the page source. What could be causing this issue? Wh ...

Is it possible that the scroll locking feature in JS/CSS does not function properly on iOS Safari when the browser interface is hidden?

Wondering if there is a solution for preventing page scroll when a simple menu button opens a semi-transparent overlay with an opacity of 0.9. I tried using techniques like setting body: overflow hidden and position fixed, but it only works on iPhone Safar ...

Regular expression for identifying a specific attribute paired with its corresponding value in a JSON object

Below is a JSON structure that I am working with: 'use strict'; // some comment is going to be here module.exports = { property1: 'value1', property2: 999, }; I am looking to remove the property2: 999, from the JSON. I attempted ...

Insertion into the aspnet_Users database

I am looking to add a new record to aspnet_Users. This user will require a unique userid and password combination. Could it be accomplished using aspnet_Membership_CreateUser to create this information? ...

Utilize React's Context Provider to centrally manage all state while incorporating async calls

I am currently exploring more refined methods to establish a provider/consumer setup in which an asynchronous call is initiated from the provider, but the consumer does not need to handle state management. Within my context provider, I am fetching data to ...

What causes the return of an empty object when using element or xpath searches with HTMLSession in requests-html?

I am looking to confirm the existence of a specific element. My approach is to print it out so that I can compare it with a string or another value. Below is the code causing issues: session = HTMLSession() r = session.get('https://www.walmart.com/ip ...

How many characters are in SCEditor? Let's calculate and display the count

Currently, I am utilizing the real simple WYSIWYG editor SCEditor for my website. I am curious about how I can precisely determine the current number of characters in its textarea and display them below it? Although I posted a question on GitHub, it seem ...

obtain data from JSON using JavaScript

Greetings! I am dealing with a JSON output that looks like this: "{ \"max_output_watts\": 150, \"frame_length_inches\": \"62.20\", \"frame_width_inches\": \"31.81\" }" I am using it in a functi ...

Facing issues with the forEach method in ReactJs when attempting to access data from an array

Struggling with a ReactJS issue, I've come across a problem with the HomePage Component where the forEach method isn't working as expected. I'm attempting to retrieve data from a defined array. Take a look at the code snippet below: import R ...

Looking for assistance in cutting down the time it takes to extract dynamic content through scraping

I'm currently using Selenium (chrome) to scrape dynamic content from a website, but the automated browser is processing too slowly. The profile page I am scraping has a "view more" button that only loads 5 posts per click. Unfortunately, there is no a ...

Is it possible for browsers to handle PUT requests using multipart/form data?

Is it common for HTML forms to not support HTTP PUT requests when submitted from certain browsers like Google Chrome? <form id="#main-form" action="http://localhost:8080/resource/1" method="put" enctype=" ...

What is the process for dynamically setting @click in vue.js?

My array contains objects with methods as actions from a vue object. How can I dynamically set the @click event in a v-for loop? I attempted to use this.m1, "this.m1", "m1", but encountered an error: fns.apply is not a function. Javascript: new Vue ...

Storing an HTML page in a PHP variable, parsing it, and displaying it correctly

I am currently using the simple_html_dom parser to extract information from an HTML page. After making some modifications, I would like to display this content on my own page. $page = file_get_html($url); foreach($page->find('div#someDIv') as ...

Page is being redirected due to a 401 status code

Setting up authorization using AngularJS and angular ui router involves handling a 401 status code from the server when a user attempts to access a protected route. An HTTP response interceptor has been created to detect the 401 status code and redirect t ...

Utilize vanilla JavaScript to invoke the Angular factory

Struggling to find the right title for this query, I'm diving into Angular and using ngMaterial. Currently, I have a toast set up through an Angular factory. app.factory('notify', ['$mdToast', '$animate', function($mdToa ...

How come I lose a day when attempting to convert a date to an ISO string in JavaScript?

I've been attempting to convert a date object to the ISOString() format, but it's consistently returning a result that is 1 day off (i.e., it subtracts 1 day). var fromDate = { day:4, month:5, year:2012 } var fromDateString = new Date ...

Is it possible to use Selenium IDE to navigate if a variable contains a specific string

Thank you for taking the time to review this. I have developed a script that scans a web page to verify if a specific text string is present. If it is not found, the script will loop. The issue I am facing is that I am required to input every single word ...

What could be causing my chosen value to remain unchanged after being copied from a controller?

I am struggling with controlling a <select> element using both ng-model and ng-options: <select ng-model="user.option" ng-options="value.label for (key, value) in data.options"> <option value="">Select value</option> ...

Ways to tally elements that have been dynamically loaded onto the webpage through AJAX requests

My page has a dynamic region that is continuously updated using jQuery's .ajax and .load methods. In order to submit the form, there need to be at least 3 of these 'regions', so I have to keep track of the number of DIVs with a specific cla ...

Ways to categorize items retrieved from an HTTP request to the backend in Angular

When making a call to the backend using this http request: this.StudentEnrollment.getRecordsById(list.value.split(/[\r\n]+/)).subscribe(values => { this.studentObject = values; }); The studentObject is structured as shown below: { recor ...