C# 您可以从页面的静态方法中检索用户控件吗?

C# 您可以从页面的静态方法中检索用户控件吗?,c#,asp.net,ajax,webmethod,C#,Asp.net,Ajax,Webmethod,我试图使用ajax/jquery从客户端调用用户控件中的方法。我的ajax看起来像这样: function starClick(starIndex) { $.ajax({ type: "POST", url: "ItemPage.aspx/postRatingProxy", data: "{}", contentType: "application/json; charset=utf-8", dataType

我试图使用ajax/jquery从客户端调用用户控件中的方法。我的ajax看起来像这样:

function starClick(starIndex) {
    $.ajax({
        type: "POST",
        url: "ItemPage.aspx/postRatingProxy",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            document.getElementById("testAjax").innerHTML = msg.d;
        }
    });
}
我的页面方法类似于:

[WebMethod]
public static string postRatingProxy()
{
    return .......postRating();
}
public static string postRating()
{
    return "git er done";
}
然后,用户控件方法如下所示:

[WebMethod]
public static string postRatingProxy()
{
    return .......postRating();
}
public static string postRating()
{
    return "git er done";
}
我在某处看到有人提出这种方法。虽然我对如何在页面方法是静态的情况下从页面方法中检索UserControl方法感到非常困惑。是否可以从静态方法中检索UserControl,或者我只是遇到了死胡同

是否可以从静态方法或 我是不是走到了死胡同

不,这是不可能的。ASP.NET页面方法是静态的,不允许您访问任何用户控件。原因很简单。使用jQuery执行AJAX请求时,不会向服务器发送ViewState,因此用户控制的概念几乎没有意义。如果需要访问Page方法中的某些值,请将该值作为AJAX请求中的参数发送:

data: JSON.stringify({ someParameter: 'some value you could take from wherevr you want' }),

如果你是对的,那么有人对我撒谎。我觉得自己很笨,但我明白了。有一次,我花了一些时间冷静下来,我回来了,马上就解决了这个问题。基本上我所要做的就是:调用return USERCONTROLNAME.postRating();由于它是静态的,您可以直接从类访问它。我失败了,很抱歉打扰你。