【问题标题】:ASP.NET AJAX Controls and Toolkit textbox autocomplete with if statementASP.NET AJAX 控件和工具包文本框使用 if 语句自动完成
【发布时间】:2015-12-03 11:47:12
【问题描述】:

我正在使用 ASP.NET AJAX 控件和工具包在 .aspx 页面中自动完成 TextBoxTextBox 用作与下拉列表相关的搜索字段。现在我只希望自动完成功能在选择下拉列表中的某个类别时显示。

对于自动完成,我有这个代码

[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;

    
}

我的下拉/文本框是这样工作的

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 + "%' ";

    }
}

我尝试像这样将 if 语句添加到我的自动完成代码中

 [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;


    }

}

然后我得到两个错误-

DepartmenAuto:并非所有代码路径都返回值

非静态字段、方法或属性“DropdownList1”需要对象引用

有什么帮助吗?

【问题讨论】:

  • 错误是不言自明的 #1 你只在 if 语句中返回一个数组字符串,如果你的条件不满足怎么办。 #2 你不能在静态方法中使用实例元素,即你的下拉列表

标签: c# asp.net ajax


【解决方案1】:

DepartmenAuto:并非所有代码路径都返回值

  • 这是因为您的return 语句现在被if 块包围,因此如果您的代码没有进入if 块,编译器将无法知道返回什么。因此,要么像以前一样删除 if 条件,要么在这些花括号之外再给出一个 return 语句。

非静态字段、方法或属性“DropdownList1”需要对象引用

  • 这是因为你不能在静态方法中使用控件。 你只能访问静态任何东西中的静态所有东西。所以你必须用其他东西来检查落下。我建议您继续检查JQuery 方面的情况。

希望这会有所帮助。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
相关资源
最近更新 更多