static void c# webmethod 不能引用用户控件?

static void c# webmethod can't reference user control?

我有一个 WebMethod,我只想将 属性 翻转为 true。

我正在使用 C# 将它放在 .aspx 页面的代码隐藏中。

public partial class Data : Base
{ 
    protected void Page_Load(object sender, EventArgs e)
    {
        this.bgc1.BGEnabled = false;
    }

    [WebMethod(EnableSession = true)]
    public static void EnableBellows()
    {
        this.bgc1.BGEnabled = true;            
    }
}

我在 .ascx.cs 文件中声明 属性:

private bool _enabled = true;

public bool BGEnabled
{
    get { return _enabled; }
    set { _enabled = value; }
}

最后我用 jQuery Ajax post 调用 WebMethod。

我收到 this 在静态 属性 中无效的错误。此错误仅在 WebMethod 中而不在 Page_Load.

我的目标是将 BGEnabled 设置为 true。如果我的方法不正确,我需要采取哪些不同的措施?我需要能够从 ajax post.

I am getting the error that this is not valid in a static property.

确实如此。实际上,在这种情况下,它是一个静态方法。

静态成员是与类型相关联的成员,而不是与类型的任何 实例 相关联的成员 - 因此在这里,我们可以直接调用 EnableBellows

Data.EnableBellows();

... 没有创建 Data 的任何实例。由于 this 是指 "the instance of the object that the method was called on",它在静态成员中没有意义。

您需要考虑您的数据从何而来,以及您将如何处理修改后的数据。请记住,Web 方法 不是 作为加载页面的一部分被调用,因此它无法访问页面上的任何控件等

基本上,我怀疑您需要重新审视网络方法和 ASP.NET 页面的整个概念。

在静态方法内部您只能访问静态 methods/objects 或在静态方法范围内声明的实例