我正在使用AjaxControlToolkit,并将其AutoComplete扩展绑定到文本框。
有了原始代码,我可以让它完美地工作。我的需求已经从仅仅查询和发送1组数据发展到必须用密钥发送数据。
例:
当用户输入某个文本时,查询搜索3个表的可能性,将所有结果发回。现在,我想将这些结果绑定到从表中提取的表中。
键不需要显示在扩展程序上,只显示值。
我的想法是将结果绑定到字典,然后循环获得值(将值绑定到string[]以返回到AutoComplete),然后使用键在选定变量来自的另一个文本框中签名。
当前代码.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
}发布于 2012-09-26 13:15:17
主要问题是您试图将重复键添加到字典中。改为使用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();提请注意从源表名称和键值本身生成的项的键。
然后,在页面上添加到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隐藏字段中获得所选值并对其进行解析。
https://stackoverflow.com/questions/12599411
复制相似问题