【问题标题】:AjaxControlToolkit AutoComplete with Dictionary带有字典的 AjaxControlToolkit 自动完成
【发布时间】:2012-09-17 22:36:44
【问题描述】:

我正在使用 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
}

【问题讨论】:

    标签: c# asp.net autocomplete ajaxcontroltoolkit


    【解决方案1】:

    主要问题是您试图将重复的键添加到字典中。请改用List&lt;KeyValuePair&lt;string, string&gt;&gt; 集合:

    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();
    

    请注意该项目的键是从源表名和键值本身生成的。

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

    <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 隐藏字段中获取选定的值并对其进行解析。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多