簡體   English   中英

數據未在ASp.net C#中的Ajax自動完成中顯示

[英]Data is not showing up in Ajax Auto complete in ASp.net C#

我有一個文本框,並且向此文本添加了AJAX AutocompleteExtender。代碼如下。來自:

ANd是我添加的CS文件

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        string connString = ConfigurationManager.ConnectionStrings["Station"].ToString();

        string selectString = "SELECT *from Station";

        List<String> CustList = new List<string>(count);
        using (SqlConnection sqlConn = new SqlConnection(connString))
        {
            sqlConn.Open();

            using (SqlCommand sqlCmd = new SqlCommand(selectString, sqlConn))
            {
                SqlDataReader reader = sqlCmd.ExecuteReader();
                while (reader.Read())
                    CustList.Add(reader["DBRT"].ToString());//DBRT is the Column name

            }
        }
        return (CustList.ToArray());
    }

當我執行代碼並輸入內容時,我輸入的內容未顯示任何內容。我的代碼有什么問題。

如果不使用Web服務,這就是我要做的。 如果需要,您也可以使用Web服務。 我不重述字符串數組,而是返回泛型List。

在Aspx文件中

您可以根據自己的喜好更改Ajax控件的標記Perfix,在我的系統中,Ajax控件的標記perfix從asp開始。

<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods = "true">
</asp:ScriptManager>

<asp:TextBox ID="StationSearchTxtBox" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ServiceMethod="GetCompletionList"
    MinimumPrefixLength="2"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="StationSearchTxtBox"
    ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</asp:AutoCompleteExtender>

在CS文件中

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]

public static List<string> GetCompletionList(string prefixText, int count)
    {

using (SqlConnection conn = new SqlConnection())
    {
       string connString =    ConfigurationManager.ConnectionStrings   ["Station"].ToString();

        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select StationName from Stations where " +
            "StationName like @SearchStation + '%'";
            cmd.Parameters.AddWithValue("@SearchStation", prefixText);
            cmd.Connection = conn;
            conn.Open();
            List<string> stations= new List<string>();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(sdr["StationName"].ToString());
                }
            }
            conn.Close();
            return stations;
        }
    }

完全希望這段代碼能給您解決問題的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM