AjaxControlToolkitを使用しており、そのオートコンプリート拡張機能をテキストボックスにバインドしています。
元のコードを使用すると、完全に機能させることができます。私の要件は、1セットのデータをクエリして送信することから、そのデータをキーで送信する必要があることへと進化しました。
例:
ユーザーがテキストを入力すると、クエリは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
}