0

I have a Static Method that is

    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetWorkPackages(string prefixText)
    {
        DataTable dt = getWorkpackages(ddlWp.SelectedValue);
        List<string> wps = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            wps.Add(dt.Rows[i][1].ToString());
        }
        return wps;
    }

Here Error Occured at ddlWp.SelectedValue(The name 'ddlWp' doesn't exist in the current context )

ddlWp is my asp control dropdownlist

How can I pass my dropdown in static method ?

shafi7468
  • 323
  • 1
  • 3
  • 15
  • 1
    That doesn't work. [You can't access controls of your page from a `WebMethod`](http://stackoverflow.com/questions/2133194/access-asp-net-control-from-static-webmethod-js-ajax-call). – Tim Schmelter Apr 19 '16 at 11:31

3 Answers3

0

Seeing as it is only being called once, you could pass the value itself

public static List<string> GetWorkPackages(string prefixText, string ddlVal)
{
    DataTable dt = getWorkpackages(ddlVal);
    List<string> wps = new List<string>();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        wps.Add(dt.Rows[i][1].ToString());
    }
    return wps;
}
Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26
0

You can return the list to ajax from where the web method is called and bind it from ajax itself. Control won't get in web method.

Alex Mathew
  • 340
  • 1
  • 3
  • 12
0

As Adil answered here

Dropdownlist object is non static member of your class (Web Page) and static methods can not access not static members. Pass the dropdownList value to static method when you call it.

Static Members

Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

Community
  • 1
  • 1
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115