2

我正在使用 AjaxControlToolkit,并将其 AutoComplete 扩展绑定到文本框。

使用原始代码,我可以让它完美地工作。我的需求已经从仅仅查询和发送一组数据演变为必须使用密钥发送该数据。

例如:
当用户输入一些文本时,查询会在 3 个表中搜索一个可能性,然后发回所有结果。我现在想将这些结果绑定到从中获取的表中。
密钥不必显示在扩展器上,只需显示值。

我的想法是将结果绑定到字典,然后遍历它以获取值(将值绑定到字符串 [] 以返回自动完成),然后使用键在另一个文本框中分配所选变量的位置从。

当前代码.aspx

<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtMainSearch" ServiceMethod="GetCompletionList" CompletionInterval="500" FirstRowSelected="True" CompletionListCssClass="completionList" CompletionListItemCssClass="listItem" CompletionListHighlightedItemCssClass="itemHighlighted"></ajaxToolkit:AutoCompleteExtender>

。CS

[WebMethod, ScriptMethod]
public static string[] GetCompletionList(string prefixText)
{
    ArrayList srings = new ArrayList();

    int counter = 0;

    SqlConnection db = DataConn.SqlConnection();

    db.Open();
    SqlTransaction transaction = db.BeginTransaction();

    Dictionary<string, string> dictionary = new Dictionary<string, string>();

    try
    {
        SqlCommand command = new SqlCommand("[Table 1]" + prefixText + "[Var]", db, transaction);
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                counter = counter + 1;
                dictionary.Add("ItemOne", reader["something"].ToString());
            }
        }

        command = new SqlCommand("[Table 2]", db, transaction);
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                counter = counter + 1;
                dictionary.Add("ItemTwo", reader["something"].ToString());
            }
        }

        transaction.Commit();
    }
    catch (SqlException)
    {
        transaction.Rollback();
        dictionary.Add("Error", "Problem Getting Results");
    }

    if (counter == 0)
        dictionary.Add("Error", "There are no users to display");

    foreach (KeyValuePair<string, string> valuePair in dictionary)
    {
        srings.Add(valuePair.Value);
    }

    return null; //This isnt really null... Just accidently deleted the code
}
4

1 回答 1

1

主要问题是您试图将重复的键添加到字典中。改为使用List<KeyValuePair<string, string>>集合:

var values = new List<KeyValuePair<string, string>>();

// select data from first table
foreach (var id in Enumerable.Range(1,10))
{
    values.Add(new KeyValuePair<string,string>("Table1_" + id.ToString(), Guid.NewGuid().ToString() );
}

//select data from the second table
foreach (var id in Enumerable.Range(1,10))
{
    values.Add(new KeyValuePair<string,string>("Table2_" + id.ToString(), Guid.NewGuid().ToString() );
}

if(values.Count == 0)
{
    values.Add(new KeyValuePair<string,string>("", "There are no users to display"));
}

return values.Select( pair => AutoCompleteExtender.CreateAutoCompleteItem(pair.Value, pair.Key)).ToArray();

提请注意从源表名称和键值本身生成的项的键。

然后,在页面上添加到 AutoCompleteExtenderOnClientItemSelected客户端事件处理程序并在表单上添加隐藏字段以存储所选项目值:

<script type="text/javascript">
     function itemSelected(sender, args) {
          $get("<%= AutoCompleteSelectedValue.ClientID %>").value = args.get_value();
     }
</script>

<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtMainSearch"
     ServiceMethod="GetCompletionList" CompletionInterval="500" FirstRowSelected="True"
     CompletionListCssClass="completionList" CompletionListItemCssClass="listItem"
     CompletionListHighlightedItemCssClass="itemHighlighted"
     OnClientItemSelected="itemSelected">

<asp:HiddenField runat="server" ID="AutoCompleteSelectedValue" />

之后,您可以从 AutoCompleteSelectedValue 隐藏字段中获取选定的值并对其进行解析。

于 2012-09-26T13:15:17.677 回答