2012-06-26 32 views
0

我已經wriiten在Asp.net一個簡單的Ajax的自動完成代碼(C#)Ajax的自動完成代碼不工作(含數據庫)

這是代碼

ASPX

<asp:TextBox ID="TextBox1" runat="server" Height="21px" Width="80px"></asp:TextBox> 
<asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" MinimumPrefixLength="1" ServiceMethod="GetCompletionList" TargetControlID="TextBox1" UseContextKey="True"></asp:AutoCompleteExtender> 

CodeBehind

當我執行並運行程序時,什麼都不會發生。我不知道出了什麼問題。請指導我。

回答

0

修改代碼,在返回部分

return Custlist; 
+0

顯示錯誤 – user1465978

+0

您能粘貼錯誤嗎? – MACMAN

0

確保您對方法這兩個屬性:

[System.Web.Services.WebMethod] 
[System.Web.Script.Services.ScriptMethod] 
public string[] GetCompletionList................{} 

編輯:的事情,我在你的代碼注意到夫婦: string selectString = "SELECT *from Station";是錯,應該是SELECT * from(*和from之間的空格缺失)。

此外,您只需選擇那些以您的方法正在接收的前綴字符串值開頭的名稱。您的方法簽名應該是:

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()] 
public static string[] GetCompletionList(string prefixText, int count, string contextKey) { 

其中prefixText是您在數據庫中查找的內容。您的選擇查詢應該是:

string selectString = "SELECT * from Station where @Name like"; 

然後定義該命令使用後: cmd.Parameters.AddWithValue(NAME,prefixText + 「%」);

這將根據從文本框中鍵入的值從表中選擇值。 它類似於: Select * from station where Name like 'some%';

另外,我建議你用Top關鍵字限制你的結果,因爲表可能包含太多的行,並根據需要自動完成的擴展不會執行。

+0

我有這兩個[System.Web.Services.WebMethodAttribute(),System.Web.Script.Services.ScriptMethodAttribute()] – user1465978

+0

@ user1465978,你得到了什麼錯誤,並把一個斷點,看看你是否從數據庫中獲取列表中的所有內容 – Habib

+0

發出錯誤並指出 未將對象引用設置爲對象的實例。 at string connString = ConfigurationManager.ConnectionStrings [「Station」]。ToString(); – user1465978