-1

In the below code i have a static method i want to access controls like gridview .But it throws object reference error.I tried like the below link :How to access page controls inside a static web method?. Pls help me to solve the issue.

     [WebMethod]
            public static void Grade()
{
     if (HttpContext.Current != null)
                {
                    Page page = (Page)HttpContext.Current.Handler;
                    GridView gd = (GridView)page.FindControl("gdgrade");//null value
                    DataSet ds = GradeSystem.GradeSystem();
                    gd.DataSource = ds.Tables[0];
                    gd.DataBind();


                }
    }
Community
  • 1
  • 1
Dotnet
  • 109
  • 12
  • Ugh, you should avoid doing this completely (assuming it's possible, which the answer you linked to suggests it isn't). If you need to bind some data to the table, have your method return the data, and have the client side format it and display it. – mason Sep 21 '15 at 18:52

1 Answers1

1

"The whole point of [WebMethod]s is that they don't run the ASP.Net page lifecycle. This way, they're fast and parallelizable. Your controls don't exist." - from the example you cited... reading the text and not just the code is often helpful ;)

If you want to load the grid, use a jquery AJAX call to a webmethod that returns the data you want:

http://www.codeproject.com/Tips/775585/Bind-Gridview-using-AJAX

Clay Sills
  • 235
  • 1
  • 9