2017-01-04 117 views
1

我想使數據庫的文本框自動完成。我用下面的代碼,但在輸出擴展顯示HTML代碼。它甚至不執行函數後面的代碼。以下代碼我使用了哪些不能正常工作。Asp.net自動完成擴展器不能正常工作

enter image description here

ASPX頁面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="_Default" %> 
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
<asp:ScriptManager ID="ScriptManager1" runat="server" 
EnablePageMethods = "true"> 
</asp:ScriptManager> 

<asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox> 
<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers" 
    MinimumPrefixLength="2" 
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" 
    TargetControlID="txtContactsSearch" 
    ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false"> 
</cc1:AutoCompleteExtender> 
    </div> 
    </form> 
</body> 
</html> 

C#代碼

[System.Web.Script.Services.ScriptMethod()] 
[System.Web.Services.WebMethod] 
public static List<string> SearchCustomers(string prefixText, int count) 
{ 
    using (MySqlConnection conn = new MySqlConnection()) 
    { 
     conn.ConnectionString = ConfigurationManager 
       .ConnectionStrings["conio"].ConnectionString; 
     using (MySqlCommand cmd = new MySqlCommand()) 
     { 
      cmd.CommandText = "select clientID from clientsDetails where " + 
      "clientID like @SearchText + '%'"; 
      cmd.Parameters.AddWithValue("@SearchText", prefixText); 
      cmd.Connection = conn; 
      conn.Open(); 
      List<string> customers = new List<string>(); 
      using (MySqlDataReader sdr = cmd.ExecuteReader()) 
      { 
       while (sdr.Read()) 
       { 
        customers.Add(sdr["clientID"].ToString()); 
       } 
      } 
      conn.Close(); 
      return customers; 
     } 
    } 
} 
+0

確保您已註冊'AjaxControlToolkit'並添加''的ScriptManager –

+0

@Div是一個已添加。 – SUN

+0

請加入問題! –

回答

0

觀看包含在URL中TextBox頁:

http://localhost:64890/CS

這似乎是由route被映射到頁面

CS.aspx

AutoCompleteExtender發送它的AJAX請求時,它會發送到這個地址:

http://localhost:64890/CS/SearchCustomers

這是由一些route可能拿起您已經成立,其結果是,它並沒有去到PageMethodCS.aspx.cs但在一些其他的方式代替處理。

假設你CS.aspx頁面在Web應用程序根文件夾,您可以通過添加此解決它:

ServicePath="~/CS.aspx" 

AutoCompleteExtender。像這樣:

<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers" 
    MinimumPrefixLength="2" 
    ServicePath="~/CS.aspx" 
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" 
    TargetControlID="txtContactsSearch" 
    ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false"> 
</cc1:AutoCompleteExtender> 
+0

沒有人。我設置擴展器的屬性就像你顯示,但現在沒有輸出顯示..我也嘗試通過創建新項目和簡單的網頁表單相同的代碼,它工作正常。在我的實際項目中,我路線。 – SUN

+0

@SUN在解決方案/ Web應用程序中,「CS.aspx」的路徑是什麼?另外,你可以添加你的路線的問題? – user1429080

+0

我的頁面名稱是auto.aspx,所以我的服務路徑是〜/ auto.apsx – SUN

0
add <cc1:ToolkitScriptManager></cc1:ToolkitScriptManager> instead of <asp:ScriptManager></asp:ScriptManager> 

your aspx page ---- 

    <cc1:ToolkitScriptManager ID="ScriptManager1" runat="server" 
    EnablePageMethods = "true"> 
    </cc1:ToolkitScriptManager> 

    <asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox> 
       <cc1:AutoCompleteExtender ServiceMethod="SearchCustomers" MinimumPrefixLength="1" CompletionInterval="10" 
       EnableCaching="false" CompletionSetCount="10" TargetControlID="txtContactsSearch" ID="AutoCompleteExtender2" 
       runat="server" FirstRowSelected="false"></cc1:AutoCompleteExtender> 


    your cs page---- 

    [System.Web.Script.Services.ScriptMethod()] 
      [System.Web.Services.WebMethod] 
      public static List<string> SearchCustomers(string prefixText, int count) 
      { 

       using (SqlConnection conn = new SqlConnection()) 
       { 
        conn.ConnectionString = ConfigurationManager 
          .ConnectionStrings["conio"].ConnectionString; 
        using (SqlCommand cmd = new SqlCommand()) 
        { 
         cmd.CommandText = "select clientID from clientsDetails where "+"clientID like @SearchText + '%'"; 
         cmd.Parameters.AddWithValue("@SearchText", prefixText); 
         cmd.Connection = conn; 
         conn.Open(); 
         List<string> customers = new List<string>(); 
         using (SqlDataReader sdr = cmd.ExecuteReader()) 
         { 
          while (sdr.Read()) 
          { 
           customers.Add(sdr["name"].ToString()); 
          } 
         } 
         conn.Close(); 
         return customers; 
        } 
       } 
      }