1

I'm using ASP.NET AJAX Controls and Toolkit to autocomplete a TextBox in a .aspx page. The TextBox works as a search field in connection with a dropdown. Now I only want the autocomplete to show when a certain category from the dropdown is selected.

For autocomplete I've got this code

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] DepartmentAuto(string prefixText, int count)
{
  
        string[] _strArray = {  "Factory Management", "Housekeeping", "HR", "Industry Development"}

        return _strArray;

    
}

And my dropdown/ textbox works like this

void Filter()
{
    if (DropDownList1.SelectedValue.ToString() == "Title")
    {

        ObjectDataSource1.FilterExpression = "Title LIKE '%" + TextBox1.Text + "%' ";

    }
    else if (DropDownList1.SelectedValue.ToString() == "Department")
    {

        ObjectDataSource1.FilterExpression = "Department LIKE '%" + TextBox1.Text + "%' ";

    }
}

I've tried to add the if statement to my autocomplete code like this

 [System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] DepartmentAuto(string prefixText, int count)
{
    if (DropDownList1.SelectedValue.ToString() == "Department")
    {
         string[] _strArray = {  "Factory Management", "Housekeeping", "HR", "Industry Development"}

        return _strArray;


    }

}

But then I get two errors-

DepartmenAuto: not all code paths return a value

An object reference is required for the non-static field, method or property "DropdownList1"

Any help how I can do this?

Community
  • 1
  • 1
  • The errors are self explanatory #1 you are returning an array string only in if statement, what if your condition is not met. #2 you cannot use instance elements in static method i.e. your dropdownlist – Zaki Dec 03 '15 at 11:53

1 Answers1

0

DepartmenAuto: not all code paths return a value

  • This is because your return statement is now surrounded by the if block and hence the compiler wont be able to know what to return if your code doesnt go into the if block. So either you remove the if condition like previously you had or your give another return statement outside those curly braces.

An object reference is required for the non-static field, method or property "DropdownList1"

  • This is because you cant use a control in a static method. You can only access static everything inside static anything. So you have to come around with something else for check on the dropdown. I suggest you go on and have a check on the JQuery side for this.

Hope this helps.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34
  • thanks, makes sense. Any suggestion on how I could go about it with jQuery? –  Dec 03 '15 at 12:44
  • See these links : http://stackoverflow.com/questions/1643227/get-selected-text-from-a-drop-down-list-select-box-using-jquery , http://stackoverflow.com/questions/10411568/how-to-get-the-text-of-the-selected-value-of-a-dropdown-list – Dhrumil Dec 03 '15 at 12:46
  • Once you check with JQuery, you can return true or false as per that and then make a postback. Otherwise if isn't your expected value, no need to even make a postback. – Dhrumil Dec 03 '15 at 12:47