[英]How can I call a non static method written in code behind using javascript?
请帮助我使用json对象从后面的代码中调用非静态方法。
在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
});
}
以下代码写在aspx.cs页面中
[WebMethod]
public static List<string> FillColumnDropdown(string selTableName)
{
//Code to update Selected Columns in table
}
因为您要调用页面的实例方法,所以我相信您想使用ASP.NET平台提供的其他页面的控件属性或功能,例如ViewState或其他一些功能。 但是,当您发出普通的ajax请求时,您将无法使用任何可能性。 因此,您使用ASP.NET Ajax的唯一选择。 在这种情况下,页面将经历整个生命周期,包括重新创建服务器上所有页面的控件实例,读取ViewState等。
通常,在使用ASP.NET时,您不需要任何自定义的javascript调用,因为所有必需的javascript都是开箱即用的。 您所需要的只是将ScriptManager和UpdatePanel控件添加到页面上,将您的下拉列表放入UpdatePanel并将其上的AutoPostback设置为true。 就这样。 之后,您可以使用该下拉列表的服务器端SelectedIndexChanged事件处理程序,它将被异步触发。
有关使用UpdatePanel的更多信息,请单击此链接: UpdatePanel控件简介
编辑:顺便说一句:如果您只需要会话或应用程序状态或缓存,但不需要页面的控件属性,也不需要ViewState,您仍然可以使用普通的ajax调用和静态服务器方法。
实际上,AJAX是非常棘手的事情:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.