我正在使用 ajax 自动完成扩展器使用服务器代码加载来自所有国家的所有城市..
一切正常.. 如果我点击“ban”,将添加一组结果.. 第一名将是班加罗尔,第二名将是 bangkog,等等。问题是如果我按下键以浏览 bangkog 和其他它会引发错误。
Javascript 运行时错误:
jaascript runtime error:System.argumentnullexception:value cannot be null.parameter name :classname in ajax autocomplete extender
aspx
<autofill:AutoCompleteExtender BehaviorID="AutoCompleteEx" ServiceMethod="GetCompletionList"
ID="fromlocation_AutoCompleteExtender" runat="server" DelimiterCharacters=""
Enabled="True" ServicePath="" TargetControlID="fromlocation" UseContextKey="True"
MinimumPrefixLength="2" CompletionInterval="10" EnableCaching="true" CompletionSetCount="20"
CompletionListItemCssClass="autocomplete_listItem">
<Animations>
<OnShow>
<Sequence>
<OpacityAction Opacity="0" />
<HideAction Visible="true" />
<ScriptAction Script="
// Cache the size and setup the initial size
var behavior = $find('AutoCompleteEx');
if (!behavior._height) {
var target = behavior.get_completionList();
behavior._height = target.offsetHeight - 2;
target.style.height = '0px';
}" />
<Parallel Duration=".4">
<FadeIn />
<Length PropertyKey="height" StartValue="0" EndValueScript="$find('AutoCompleteEx')._height" />
</Parallel>
</Sequence>
</OnShow>
<OnHide>
<Parallel Duration=".4">
<FadeOut />
<Length PropertyKey="height" StartValueScript="$find('AutoCompleteEx')._height" EndValue="0" />
</Parallel>
</OnHide>
</Animations>
</autofill:AutoCompleteExtender>
C#
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["connstring"].ToString());
//SqlCommand cmd = new SqlCommand("SELECT Code,City FROM CCode WHERE City LIKE '" + prefixText + "%'", conn);
SqlCommand cmd = new SqlCommand("SELECT coalesce(Code + ', ', '') + City as codes FROM CCode WHERE City LIKE '" + prefixText + "%'", conn);
SqlDataReader oReader;
conn.Open();
List<string> CompletionSet = new List<string>();
oReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (oReader.Read())
CompletionSet.Add(oReader["codes"].ToString());
return CompletionSet.ToArray();
}