asp.net - Webmethod 引发 "Cannot refer to an instance member of a class from within a shared method or shared member..."

标签 asp.net vb.net asmx webmethod pagemethods

我有以下网络方法

<System.Web.Services.WebMethod()> Public Shared Function NewMethod(ByVal str1 As String) As String
  LoadBlock(str1)
  Return "Success"
End Function

以及以下过程

Public Sub LoadBlock(ByVal AA As Integer)
  panAway.Visible = False '<--ASP panel control
  lblHome.Visible = False '<--ASP label control
End

这会引发以下错误

Cannot refer to an instance member of a class from within a shared method or 
shared member initializer without an explicit instance of the class.

我知道我收到以下错误,因为我尚未将 LoadBlock 设置为共享

但是如果我这样做了我的程序中的所有控件...会引发上面的错误

我能做些什么吗?

最佳答案

“页面方法”始终是共享(C# 中的静态)方法。它永远不能引用页面上的控件,也不能引用任何其他实例数据或方法。

事实上,请记住,在 ASP.NET 中,页面的实例仅在每个 HTTP 请求期间存在。对 WebMethod 的请求是一个单独的请求 - 没有它可能引用的页面实例。当对 WebMethod 的请求到达时,该实例已被销毁。

关于asp.net - Webmethod 引发 "Cannot refer to an instance member of a class from within a shared method or shared member...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19343694/

相关文章:

.net - Elmah-捕获错误,但仍然记录并发送电子邮件?

ASP.NET 身份数据库第一个自定义用户和角色

MySQL 服务器和 VB 2008 安全

asp.net - 当从本地计算机而不是远程查看时,ASMX 测试页面将 HTTP POST 作为选项显示?

c# - 使用 NTLM 身份验证的基于 HTTP 的 Web 服务

asp.net - asmx Web 服务是否与 REST 兼容?

asp.net - 我有一个关于 ViewState 的问题

asp.net - OpenAM、OpenId、REST API、内部应用程序 : how do I connect them all?

vb.net - IsNumeric() 允许在最后一个值中减去

asp.net - 如何使ASP文件上传控件接受大文件?