简体   繁体   中英

How can I call a non static method written in code behind using javascript?

Kindly help me to call a non-static method from code behind, using json object.

The following code is used in aspx page:

 function columnDropdownScript() {
                if (id == "ExpressionsLink") {
                    var dropdown = document.getElementById("<%=ddlTableNames.ClientID%>");
                }

                /

/ Configure AJAX call to server on itemChange of ddlTableNames
                $.ajax({
                    type: "POST",
                    url: "Webtop.aspx/FillColumnDropdown",
                    data: requestTableParameters,
                    //contentType: "plain/text",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        if (id == "ExpressionsLink") {
                            $("#ddlColumnNames").empty();

                            }
                        }
                        //  alert('Check Column DropDown' + msg.d.length);
                    },
                    error: DisplayError //Event that'll be fired on Error
                });

            }                                                                   

The following code is written in aspx.cs page

[WebMethod]
    public static List<string> FillColumnDropdown(string selTableName)
    {

       //Code to update Selected Columns in table
    }             

Since you want to call page's instance method I believe that you want to use other page's controls properties or features provided by an ASP.NET platform like ViewState or some other. But when you fire plain ajax request you can't use any of that possibilities. So the only one option for you to use ASP.NET Ajax. In that case page comes through full life cycle including recreation all page's controls instances on the server, reading ViewState and so on.

Mostly for using ASP.NET you don't need any custom javascript calls because all required javascript provided out of the box. What do you need it's just add ScriptManager and UpdatePanel controls onto the page, put your dropdown in UpdatePanel and set AutoPostback on it to true. That's all. After that you can use server-side SelectedIndexChanged event habdler of that dropdownlist and it will be fired asynchronously.

For more info about using an UpdatePanel follow this link: Introduction to the UpdatePanel Control

EDIT: by the way: if you need only the Session or Application state or Cache but no page's controls properties and no ViewState you still can use plain ajax calls and static server methods.

Actually the AJAX is quite tricky thing :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM