我有以下网络方法
<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/