简体   繁体   English

如何使用javascript调用用代码编写的非静态方法?

[英]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. 请帮助我使用json对象从后面的代码中调用非静态方法。

The following code is used in aspx page: 在aspx页面中使用以下代码:

 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 以下代码写在aspx.cs页面中

[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. 因为您要调用页面的实例方法,所以我相信您想使用ASP.NET平台提供的其他页面的控件属性或功能,例如ViewState或其他一些功能。 But when you fire plain ajax request you can't use any of that possibilities. 但是,当您发出普通的ajax请求时,您将无法使用任何可能性。 So the only one option for you to use ASP.NET Ajax. 因此,您使用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. 在这种情况下,页面将经历整个生命周期,包括重新创建服务器上所有页面的控件实例,读取ViewState等。

Mostly for using ASP.NET you don't need any custom javascript calls because all required javascript provided out of the box. 通常,在使用ASP.NET时,您不需要任何自定义的javascript调用,因为所有必需的javascript都是开箱即用的。 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. 您所需要的只是将ScriptManager和UpdatePanel控件添加到页面上,将您的下拉列表放入UpdatePanel并将其上的AutoPostback设置为true。 That's all. 就这样。 After that you can use server-side SelectedIndexChanged event habdler of that dropdownlist and it will be fired asynchronously. 之后,您可以使用该下拉列表的服务器端SelectedIndexChanged事件处理程序,它将被异步触发。

For more info about using an UpdatePanel follow this link: Introduction to the UpdatePanel Control 有关使用UpdatePanel的更多信息,请单击此链接: UpdatePanel控件简介

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. 编辑:顺便说一句:如果您只需要会话或应用程序状态或缓存,但不需要页面的控件属性,也不需要ViewState,您仍然可以使用普通的ajax调用和静态服务器方法。

Actually the AJAX is quite tricky thing :) 实际上,AJAX是非常棘手的事情:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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