Webmethod 引发“无法从共享方法或共享成员中引用类的实例成员...”

OrE*_*lse 2 vb.net asp.net asmx pagemethods webmethod

我有以下网络方法

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

以及以下程序

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

这会引发以下错误

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.
Run Code Online (Sandbox Code Playgroud)

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

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

我能做些什么吗?

Joh*_*ers 5

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

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