【问题标题】:Webmethod raises "Cannot refer to an instance member of a class from within a shared method or shared member..."Webmethod 引发“无法从共享方法或共享成员中引用类的实例成员......”
【发布时间】:2013-10-13 09:29:48
【问题描述】:

我有以下网络方法

<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 设置为共享

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

对此我有什么办法吗?

【问题讨论】:

  • 仅供参考,WebMethod 不是“引发”错误——这是一个简单的编译器错误,并且已经告诉你需要知道的内容——假设你知道什么是“实例方法”和“共享”方法”的意思。

标签: asp.net vb.net asmx webmethod pagemethods


【解决方案1】:

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

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

【讨论】:

  • 这意味着我锁了抽屉并把钥匙放在里面了? :-) webmethod 由 ajax 回发调用...但现在看来我还有另一个问题:-(
猜你喜欢
  • 2018-12-30
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
  • 2014-04-18
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
相关资源
最近更新 更多