Invoke a backend function from the front end

In my implementation using Master and Content page, I have a client side function:

<script type="text/javascript">
    function StateChange(txtState) {
        var state = document.getElementById(txtState);
        PageMethods.StateSelectionChange(state.value, OnSuccess, onFailed);
    }
    function OnSuccess(state, userContext, methodName) {
        alert(state);
    }
    function onFailed(state, userContext, methodName) {
        alert(state);
    }
</script>

accompanied by server side code:

public static string StateSelectionChange(string state)
{
    //txtCity.Text = "";
    //if (txtState.Text != "")
    //{
    //    SqlDataReader dr = cm.Allstate(txtState.Text);
    //    if (dr.Read())
    //    {
    //        con.Close();
    //        AutoCompleteExtender2.ContextKey = txtState.Text;
    //        txtCity.Enabled = true;
    //        txtCity.Focus();
    //    }
    //    else
    //    {
    //        con.Close();
    //        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('State is not exist');", true);
    //        txtState.Text = "";
    //        txtState.Focus();
    //    }
    //}
    return (state);
}

I am facing an issue where I cannot access the commented part of the code (textbox and class main function) due to declaring the function as static. Removing the static word from the function would prevent it from being called from the client side.

My ultimate goal is to be able to call a non-static function from the client side through JavaScript/ajax request.

Answer №1

It is not possible to perform that action due to the nature of controls being instances, restricting access to only static members from static methods. A solution I often employ is passing control values as parameters to server-side methods.

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

Utilizing the filter function iteratively within an Angular factory component

I am facing an issue where I have a factory with 2 controllers. The first controller returns an entire array, while the second controller is supposed to return only one item based on a specific filtering expression. I need to extract the last two parameter ...

Async/Await moves on to the next function without waiting for the previous function to finish executing

I am developing a web application that requires querying my database multiple times. Each query depends on the data retrieved from the previous one, so I need to ensure each call completes before moving on to the next. I have attempted using async/await fo ...

Could someone clarify why EventEmitter leads to issues with global variables?

I recently encountered an error that took me some time to troubleshoot. Initially, I decided to create a subclass of EventEmitter In the file Client.js var bindToProcess = function(func) { if (func && process.domain) { return process.domai ...

Exploring the Beauty of Cubes within Three.JS

Recently delving into the world of THREE.js, I've set out on a mission to create a cube composed entirely of cubes, much like this example: https://i.sstatic.net/hQRoB.jpg To achieve this, I've structured my 3D array as follows: [[grid][line,li ...

Here's a tip on how to trigger a function when the text in an input box changes using JavaScript

While using Vue, I am developing a form which includes an input box. Whenever the text in the input box changes, it should update the function setMessage. <input type="text" v-model="test" v-on:change"setMessage"> However, the issue is that the upd ...

Overuse of jQuery's preventDefault() and stopPropagation() functions

A recent discussion with a coworker revealed some contrasting coding practices, mainly concerning his extensive use of the two aforementioned methods in event handlers. Every event handler he creates follows this same pattern... $('span.whatever&apos ...

Steps for interacting with a button of the <input> tag in Selenium using Python

As I attempt to complete a form submission, I encounter an issue where clicking the submit button does not produce any action. It seems that the problem lies with the button being tagged as <input>: <input type="submit" name="submit ...

Unable to view the Properties tab for a Web Application project within Visual Studio 2010

I feel like this might be a silly question, but I can't seem to locate the "Properties" window in order to adjust the IIS settings for my web application project. I am using Visual Studio 2010 (SP1). Strangely enough, I can access the properties windo ...

React-Redux-Saga: Only plain objects are allowed for actions. Consider using custom middleware for handling asynchronous actions

Struggling to integrate redux-saga into my react app, I keep encountering this error: Actions must be plain objects. Use custom middleware for async actions. The error appears at: 15 | changeText = event => { > 16 | this.props.chan ...

Slide your finger upwards to shut down the mobile navbar in bootstrap

Hey there, I'm currently developing a website using Bootstrap framework v3.3.4. I have a mobile navbar that opens when I click a toggle button, but I would like it to slide up to close the navigation instead. Here is an image showing the issue Do y ...

Using AngularJS to serve $resource through https causes Angular to transition from https to http

After successfully setting up a ng-resource in my AngularJS project to fetch data from a REST API, everything was running smoothly in the development and testing environments, both of which operated over http. However, upon moving to production where the e ...

When utilizing document.addEventListener in JavaScript, Firefox fails to report exceptions triggered by DOMContentLoaded

I'm developing an internal framework that must be independent of any external dependencies such as jQuery. I'm working on implementing a custom DOM ready-style functionality, where callbacks in the ready queue should continue executing even if an ...

Using the 'client-side rendering' and runtime environment variables in Next.js with the App Router

In the documentation for next.js, it's mentioned that runtime environment variables can be utilized on dynamically rendered pages. The test scenario being discussed involves a Next.js 14 project with an app router. On the page below, the environment ...

Subfolder is not generating the batch file

I need help troubleshooting this code. I created a temporary folder in the C drive, but for some reason the batch file isn't getting created. Can anyone provide some suggestions or insights? var sText, s; var fso = new ActiveXObject("Scripting.Fi ...

Different Methods of Joining Tables in SQLike

Struggling with creating a two-level JOIN in SQLike, currently stuck at the one level JOIN. The source tables are JSONs: var placesJSON=[{"id":"173","name":"England","type":"SUBCTRY"},{"id":"580","name":"Great Britain","type":"CTRY"},{"id":"821","name":" ...

The player remains unchanged

Hi, I am currently working on my app to update a player's age. To start off, I have added three players: const playerOne = store.dispatch(addPlayer({ firstName: 'Theo', lastName: 'Tziomakas', position: 'Goakeeper ...

Using Laravel to fetch data from the controller and pass it to the view using AJAX

I'm currently working on retrieving data from a controller and sending it via ajax. The script is located in app.blade.php. Controller public function displayProductNumber() { $count = Order::where('user_id', Auth::user()->id)-> ...

What is the best way to deploy a REST API utilizing an imported array of JavaScript objects?

I have been using the instructions from this helpful article to deploy a Node REST API on AWS, but I've encountered a problem. In my serverless.yml file, I have set up the configuration for the AWS REST API and DynamoDB table. plugins: - serverless ...

What are some tips for incorporating vue.js into a basic web application?

I've been trying to incorporate vue.js into my web application, but for some reason, it's not loading and the HTML is not firing in the browser. Please take a look at the code snippet below: <!DOCTYPE html> <html> < ...

An easy way to export static images in Next.js

Whenever I try to export my nextjs app, an error message pops up stating that image export is not supported on static websites. Error: The default image loader in Next.js cannot be used with next export. Possible solutions: - Use next start to run a serv ...