0

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. How can I do this?

Both of these methods are within the ASP page's cs file.

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

    [WebMethod]
    public static void MyWebMethod()
    {
        // This is the part I need help with...
        DoIt();
    }
2
  • Are you able to pass in a reference to the object you need to call it's method on? You can define the method as an interface so you can call it from multiple pages. Commented Aug 7, 2013 at 13:07
  • The object he's looking for does not exist. Commented Aug 7, 2013 at 13:19

2 Answers 2

5

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.

If you could call the instance method from the web method, that would mean that the instance method should be a static method. Can you just add static to that method and have it still work? 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. By the time your client-side code is calling the web service, that HTTP request is already over, and the page instance is gone.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way of accessing the current asp page instance so I can call it that way? I am attempting to change asp control values within the instance method thus I cant make it static since the controls are not static.
There is no current page instance.
2

You can't do it, but the txtFindingNum.Text field is input, and you can change it in client side (it also keep the change in the server after postback), with js or jq, like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.