访问code从身后功能的WebMethod [英] Accessing code behind functions from WebMethod

查看:118
本文介绍了访问code从身后功能的WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有几个方法页后面的code;其中之一是一个页面方法

I have a code behind page that has several method; one of them is a page method.

[WebMethod]
public static void ResetDate(DateTime TheNewDate)
{
    LoadCallHistory(TheNewDate.Date);
}

protected void LoadCallHistory(DateTime TheDate)
{ bunch of stuff }

LoadCallHistory工作正常时,页面加载,我可以从页面内其他方法调用它的方法。然而,在Web方法的一部分,它被红色下划线出现错误的对象引用是必需的非静态字段。

The method LoadCallHistory works fine when the page loads and I can call it from other methods inside the page. However, in the web method part, it gets underlined in red with the error "an object reference is required for the non-static field".

你如何从code的页面方法部分访问功能?

How do you access functions from the page method part of the code?

感谢。

推荐答案

您不能调用从静态上下文的非静态方法,而无需类的一个实例。要么删除静态 ResetDate 或使 LoadCallHistory 静态

You cannot call a non-static method from a static context without having an instance of the class. Either remove static from ResetDate or make LoadCallHistory static.

不过,如果删除静态 ResetDate 你必须有它的一个实例使用该方法。另一种方法是创建一个类的实例中的 ResetDate ,并使用该实例调用 LoadCallHistory ,这样的事情:

However, if you remove static from ResetDate you must have an instance of it to use that method. Another approach is to create an instance of the class inside ResetDate and use that instance to call LoadCallHistory, something like this:

[WebMethod]
public static void ResetDate(DateTime TheNewDate)
{
    var callHistoryHandler = new Pages_CallHistory();
    callHistoryHandler.LoadCallHistory(TheNewDate.Date);
}

该错误信息表明 ResetDate 的关键字静态 LoadCallHistory 没有。当使用静态或者二者的方法必须是静态或调用的方法必须是 静态,调用者不能是静态的,如果被调用的方法是没有的。

The error message indicates that ResetDate has the keyword static and LoadCallHistory does not. When using static either both of the methods needs to be static or the called method needs to be static, the caller cannot be static if the called method is not.

要在静态类和静态类成员引述MSDN

一个静态类基本相同
  作为非静态类,但有
  一个区别:静态类不能
  被实例化。换句话说,就
  不能使用new关键字来创建
  类类型的的变量。因为
  有没有实例变量,你
  访问静态类成员
  通过使用类名称本身。

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

这篇关于访问code从身后功能的WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆