Reputation: 3924
I am using AjaxControlToolkit, and binding its AutoComplete extension to a text box.
With the original code, I can get it to work perfectly. My requirements have evolved from just querying and sending 1 set of data, to having to send that data with a key.
Eg:
When the user enters some text, the query searches 3 tables for a likelihood, the sends back all the results. I want to now bind these results to the table it was taken from.
The key doesn't have to show on the extender, only the value.
My thoughts were to bind the results to a Dictionary, then loop through it to get the values (binding the values to a string[] to return to the AutoComplete), then using the Key to asign in another text box where the selected Variable came from.
Current code .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
}
Upvotes: 2
Views: 2011
Reputation: 22468
the main problem is that you attempting to add duplicate keys into dictionary. Use List<KeyValuePair<string, string>>
collection instead:
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();
Draw attention that item's key generated from source table name and key value itself.
Then, on page add to AutoCompleteExtender OnClientItemSelected
client event handler and add hidden field on form to store selected item value:
<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" />
After that you may get selected value from AutoCompleteSelectedValue hidden field and parse it.
Upvotes: 1