我不認爲你可以在一個ASCX控件的WebMethod。 我解決它爲我的問題是這樣的:
AJAXBridge:
namespace Demo{
public partial class AjaxBridge : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod(EnableSession = true)]
public static string Control_GetTest()
{
return Control.GetTest();
}
}}
Control.ascx.cs
namespace Demo{
public partial class Control : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
HttpContext.Current.Session["test"] = DateTime.Now.ToString();
}
// ALMOST A WEB METHOD
public static string GetTest()
{
return " is " + HttpContext.Current.Session["test"];
}
}}
Control.ascx
<script type="text/javascript">
var dataSend = {};
$.ajax({
type: "POST",
url: "AjaxBridge.aspx/Control_GetTest",
data: dataSend,
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function(data) {
alert("before");
},
success: function(data) {
alert("Page Load Time from Session " + data.d);
},
fail: function() {
alert("fail");
}
}); </script>
所以,你有一個ASPX它基本上就像所有Web控件中的所有AJAX方法的接口一樣。還有一些優點,例如對所有暴露的WebMethods進行概述和控制,這使得處理安全問題變得更容易(例如使用註釋)。
是的網絡方法是靜態的。如果我將它作爲標準aSPX頁面,而不是作爲Web用戶控件,則同樣的作品。 順便說一句我正在使用ASP.NET 2.0與JQuery – KK99 2010-04-25 08:14:59
@KarthikK,然後我支持我的答案,你不能將它們移動到用戶控件,並讓他們工作。 – drs9222 2010-04-25 11:37:49