如何从网络方法调用非静态方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从网络方法调用非静态方法相关的知识,希望对你有一定的参考价值。
在我的代码中,以前我在单击按钮时保存数据,这里hdnDisplayOrderSaveData
是ASP隐藏字段
protected void btnSaveDisplayOrder_Click(object sender, EventArgs e)
string data = hdnDisplayOrderSaveData.Value;
Service service = new Service();
if (IsSaveStringValid(data))
{
bool result = service.SaveServicesDisplayOrder(data);
if (result)
{
ClientScript.RegisterStartupScript(typeof(Page), "script", "showMessage(1);", true);
}
else
{
ClientScript.RegisterStartupScript(typeof(Page), "script", "showMessage(2);", true);
}
}
LoadDisplayOrder();
}
然后,我需要将上述功能移至Web方法,所以我的代码如下,
public partial class ProductAdminSortOrder : BasePage
{
private List<int> serviceIds = new List<int>();
protected void Page_Load(object sender, EventArgs e)
{
try
{
CheckSession();
CheckPagePermissions(Permissions.ServicesAdministrator);
LoadDisplayOrder();
}
catch (Exception ex)
{
Logger.LogErrorEvent(ex);
throw;
}
}
/// <summary>
/// Loads the display order.
/// </summary>
private void LoadDisplayOrder()
{
}
[WebMethod]
public static void SaveOrder()
{
string data = hdnDisplayOrderSaveData.Value;
Service service = new Service();
if (IsSaveStringValid(data))
{
bool result = service.SaveServicesDisplayOrder(data);
if (result)
{
ClientScript.RegisterStartupScript(typeof(Page), "script", "showMessage(1);", true);
}
else
{
ClientScript.RegisterStartupScript(typeof(Page), "script", "showMessage(2);", true);
}
}
LoadDisplayOrder();
}
//protected void btnSaveDisplayOrder_Click(object sender, EventArgs e)
//}
private bool IsSaveStringValid(string data)
{
data = Regex.Replace(data, @"s+", "");
string[] items = data.Split('#');
if (!IsDataValid(item))
{
return false;
}
}
private bool IsDataValid(string[] item)
{
if (item.Length != 2)
{
return false;
}
else
{
return serviceIds.Any(id => id == Convert.ToInt32(item[0], CultureInfo.InvariantCulture));
}
}
}
此后,我遇到以下错误,
An object reference is required for the non-static field, method, or property 'ServicesAdminSortOrder.hdnDisplayOrderSaveData'
An object reference is required for the non-static field, method, or property 'ServicesAdminSortOrder.LoadDisplayOrder()'
An object reference is required for the non-static field, method, or property 'Page.ClientScript'
- 我有两个问题,是web方法应该是
static
- 如何避免这些错误?
已更新:
我如何称呼网络方法,
function DeleteKartItems() {
$.ajax({
type: "POST",
url: 'ProductAdminSortOrder.aspx/SaveOrder',
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
console.log('success');
},
error: function (e) {
console.log('failed');
}
});
}
答案
None以上是关于如何从网络方法调用非静态方法的主要内容,如果未能解决你的问题,请参考以下文章