简体   繁体   English

从静态Web方法调用ASP.NET页面非静态方法

[英]Calling ASP.NET page non-static methods from static web methods

I am in need of calling a non-static method in the active(current) asp page that the user is on by using a static WebMethod. 我需要通过使用静态WebMethod在用户打开的活动(当前)asp页面中调用非静态方法。 How can I do this? 我怎样才能做到这一点?

Both of these methods are within the ASP page's cs file. 这两种方法都在ASP页的CS文件中。

    public void NormalMethod()
    {
        txtFindingNum.Text = "Ajax is da bomb";
    }

    [WebMethod]
    public static void MyWebMethod()
    {
        // This is the part I need help with...
        DoIt();
    }

You can't do it. 你做不到 It doesn't even make sense. 这甚至没有意义。

The instance methods of the page are about a specific instance of the page. 页面的实例方法与页面的特定实例有关。 When you're in the static web method (page method), there is no instance of the page. 当您使用静态Web方法(页面方法)时,没有页面实例。

If you could call the instance method from the web method, that would mean that the instance method should be a static method. 如果可以从Web方法中调用实例方法,则意味着该实例方法应该是静态方法。 Can you just add static to that method and have it still work? 您可以仅将static方法添加到该方法中并使其仍然起作用吗? If not, then it depends on the particular instance of the page, and you simply can't call it when there is no instance. 如果不是,则取决于页面的特定实例,并且当没有实例时,您根本无法调用它。


Note that a page instance exists only during the HTTP request that it is serving. 请注意,页面实例仅在其正在服务的HTTP请求期间存在。 By the time your client-side code is calling the web service, that HTTP request is already over, and the page instance is gone. 在您的客户端代码调用Web服务时,该HTTP请求已经结束,并且页面实例消失了。

您无法执行此操作,但是输入了txtFindingNum.Text字段,您可以使用js或jq在客户端(在回发后将更改保存在服务器中)进行更改,如下所示:

$("#<%=txtFindingNum.ClientID%>").val("Ajax is da bomb");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM