如何通过ASP.NET的WebMethod访问并实现函数背后的代码逻辑?

2026-03-30 11:561阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计406个文字,预计阅读时间需要2分钟。

如何通过ASP.NET的WebMethod访问并实现函数背后的代码逻辑?

我有一个代码隐藏页面,有以下几种方法;其中一种是页面方法。+ [WebMethod] public static void ResetDate(DateTime TheNewDate) { LoadCallHistory(TheNewDate.Date); } protected void LoadCallHistory(DateTime TheDate) { ... } 当“

我有一个代码隐藏页面,有几种方法;其中一个是页面方法.

[WebMethod] public static void ResetDate(DateTime TheNewDate) { LoadCallHistory(TheNewDate.Date); } protected void LoadCallHistory(DateTime TheDate) { bunch of stuff }

当页面加载时,方法LoadCallHistory工作正常,我可以从页面内的其他方法调用它.但是,在Web方法部分中,它以红色加下划线,并显示错误“非静态字段需要对象引用”.

如何从代码的页面方法部分访问函数?

谢谢.

如果没有类的实例,则无法从静态上下文中调用非静态方法.从ResetDate中删除静态或使LoadCallHistory静态.

但是,如果从ResetDate中删除静态,则必须具有该实例才能使用该方法.另一种方法是在ResetDate中创建类的实例,并使用该实例调用LoadCallHistory,如下所示:

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

错误消息指示ResetDate具有关键字static而LoadCallHistory不具有.当使用静态时,两个方法都需要是静态的,或者被调用的方法需要是静态的,如果被调用的方法不是静态的,则调用者不能是静态的.

在“Static Classes and Static Class Members”引用MSDN

如何通过ASP.NET的WebMethod访问并实现函数背后的代码逻辑?

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.

本文共计406个文字,预计阅读时间需要2分钟。

如何通过ASP.NET的WebMethod访问并实现函数背后的代码逻辑?

我有一个代码隐藏页面,有以下几种方法;其中一种是页面方法。+ [WebMethod] public static void ResetDate(DateTime TheNewDate) { LoadCallHistory(TheNewDate.Date); } protected void LoadCallHistory(DateTime TheDate) { ... } 当“

我有一个代码隐藏页面,有几种方法;其中一个是页面方法.

[WebMethod] public static void ResetDate(DateTime TheNewDate) { LoadCallHistory(TheNewDate.Date); } protected void LoadCallHistory(DateTime TheDate) { bunch of stuff }

当页面加载时,方法LoadCallHistory工作正常,我可以从页面内的其他方法调用它.但是,在Web方法部分中,它以红色加下划线,并显示错误“非静态字段需要对象引用”.

如何从代码的页面方法部分访问函数?

谢谢.

如果没有类的实例,则无法从静态上下文中调用非静态方法.从ResetDate中删除静态或使LoadCallHistory静态.

但是,如果从ResetDate中删除静态,则必须具有该实例才能使用该方法.另一种方法是在ResetDate中创建类的实例,并使用该实例调用LoadCallHistory,如下所示:

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

错误消息指示ResetDate具有关键字static而LoadCallHistory不具有.当使用静态时,两个方法都需要是静态的,或者被调用的方法需要是静态的,如果被调用的方法不是静态的,则调用者不能是静态的.

在“Static Classes and Static Class Members”引用MSDN

如何通过ASP.NET的WebMethod访问并实现函数背后的代码逻辑?

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.