Search code examples
asp.netwebformsasmxpagemethods

Callling business logic layer method from PageMethods


I've a static page method in web form application and I want to call method on private class level variable from it as shown below. I'm using jQuery to call the page method.

private readonly ICatalogBLL _catalogBLL = new CatalogBLL();

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    _catalogBLL.GetSomething();
  }
}

[WebMethod]
public static UpdateSomething(int i)
{
   //Want to do as below. But can't call it from a static method.
   _catalogBLL.UpdateSomething();
}

UPDATE
If I call it as said by John Saunders, won't it use the same instance for requests from different users as it is within a static method?


Solution

  • You can't. The page method is static. Your _catalogBLL is an instance member.

    However, since you create a new instance of CatalogBLL on every request, why not do so once more?

    [WebMethod]
    public static UpdateSomething(int i)
    {
       CatalogBLL catalogBLL = new CatalogBLL();
       catalogBLL.UpdateSomething();
    }