Access code for features from WebMethod

I have code behind page that has several methods; one of them is the page method.

[WebMethod]
public static void ResetDate(DateTime TheNewDate)
{
    LoadCallHistory(TheNewDate.Date);
}

protected void LoadCallHistory(DateTime TheDate)
{ bunch of stuff }

The LoadCallHistory method works great when loading a page, and I can call it from other methods inside the page. However, in the part of the web method, it is underlined in red with the error "an object reference is required for a non-static field."

How do you access functions from part of the code page method?

Thank.

+3
source share
2 answers

You cannot call a non-static method from a static context without having an instance of the class. Remove staticfrom ResetDateor make LoadCallHistorystatic.

, static ResetDate, , . - ResetDate LoadCallHistory, - :

[WebMethod]
public static void ResetDate(DateTime TheNewDate)
{
    var callHistoryHandler = new Pages_CallHistory();
    callHistoryHandler.LoadCallHistory(TheNewDate.Date);
}

, ResetDate static LoadCallHistory. , static, , .

MSDN

, : . , . , .

+11

static, static .

CallHistory ( ;)), :

[WebMethod]
public static void ResetDate(DateTime TheNewDate)
{
    var thisPage = new CallHistory();
    thisPage.LoadCallHistory(TheNewDate.Date);
}

LoadCallHistory static.

+1

Source: https://habr.com/ru/post/1791081/


All Articles