0

I have an aspx page and in the script section, I write an Ajax function to pass data to aspx.cs page. But the thing is ajax works with static functions. But I want to send data to the non-static function in the aspx.cs page. Is there any way to pass data from the javascript function to the non-static function in the code behind?

javascript function:

           function loadNewNode(val) {
            var parms = "{'employeeNumber': '" + val + "'}";



            $.ajax({
                type: "POST",
                async: true,
                url: "xxxxxxxx.aspx/LoadDownNode",
                data: parms,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {


                },
                error: function (e) {
                    alert("Unexpected error has occured!");
                }
            });
        }

aspx.cs(code behind function):

    public  void LoadDownNode(string employeeNumber) //not working with ajax because this is non static
    {

        string procCorpLevel = "true";
    }
1

1 Answer 1

0

But when you call such code, you don't have a instance of the class behind. It does not exist on the server or even in the web server memory.

The code behind and web page on the server? Once it dished out the page to you and your browser? The code behind page, the memory, the variableds, and the existance of hte class is NOW GONE AND DELETED.

The web server is NOT just for you! - you can have many users. That web server is simple sitting there. If someone hits a button on their page, then a post-back occures. Your web page travels up to the ONE web server sitting there wating for a post-back.

On post-back, your WHOLE web page is now sitting on the server. The page class is created, code behind starts to run. You update that page class + controls. The page then travels back to the client AND ON THE SERVER the page is removed, done, and does not even eixst anymore. The web server is now waiting for any user - you, or 1 of the other 30 users to post back a page.

So, how can you call a non static class in the code behind when it does not even exist anymore?

Your static class can most certanly use any object or class you have in your code - but you would LIKE ALL objects have to first create a instance of that class before you use it, right?

on a ajax call, you can call that routine in code behind, but it does not have the whole web page, so no in memory instnace of that class is created. Thus, you limited to static methods.

but, if you need to use some control values in that ajax call? Then in js code, you get/grab the text box or values, and then PASS the value(s) to the static method. The static method can do whatever, and return some values or data. Your js code now can update some controls on your web page.

So, the static method you call? It can most certainly use any class you have created, but it not going to create the current page class with controls, since that page has NOT been posted back to the server, and the so called "round trip" of the web page has not yet occurred.

If your code behind needs use of other controls on that page? Then you need to post-back all that page data for this to work.

So, keep the life cycle or so called round trip in mind when you write code. Without this basic graps of how a post-back works, then you not grasping the most basic level as to how web software works.

This is also why we often refer to web software as state-less. The web page is sitting on the users desktop. They might then browse to google, or close their laptop lid. The server does not care, or even know - and that web page on the server nor the class DOES NOT EVEN exist in memory.

So this is not like desktop where each user has their own computer ONCE the web server sends the web page to the client side - the server side web page is GONE, removed from memory. The web server is just sitting there waiting for anyone of your 50 users to post-back a whole web page. So, ONLY during the short time the web page is up on the server does the page class and code and variables exist. The code behind runs, has full use of the page class. But RIGHT AFTER the web server sends the web page back to the client? The web server dumps out the web page - ready now to accept any posted back web page from ANY user - not just you the one user.

Perhaps you would be better to ask what you want to do with the ajax call and method, but such calls certainly don't care or have ANY current knowledge of the current web page class - since it does not exist, and will ONLY exist if you post-back the whole web page, so the class instance can be created for that SHORT TIME the page exists up on the server. Once page is rendered and send to client, then the server side web page does not even exist anymore. The web server can no go to work processing other web pages (and their class) for each of the users.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.