2016-08-12 126 views
1

我需要自動完成文本框。我試着下面的代碼段:自動完成文本框出錯

<input type="text" id="tbx_srchByFn" class="autosuggest" disabled="disabled" value="fieldname" runat="server" 
            onclick="this.value=''" /> 

jQuery的

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" 
     rel="stylesheet" type="text/css" /> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      SearchText(); 
     }); 
     function SearchText() { 
      $(".autosuggest").autocomplete({ 
       source: function (request, response) { 
        $.ajax({ 
         type: "POST", 
         contentType: "application/json; charset=utf-8", 
         url: "DDL_Home.aspx/GetAutoCompleteData", 
         data: "{'username':'" + document.getElementById('tbx_srchByFn').value + "'}", 
         dataType: "json", 
         success: function (data) { 
          response(data.d); 
         }, 
         error: function (result) { 
          alert("Error"); 
         } 
        }); 
       } 
      }); 
     } 
    </script> 

.cs文件

using System.Web.Services; 
    [WebMethod] 

    public static List<string> GetAutoCompleteData(string username) 
    { 
     List<string> result = new List<string>(); 
     using (SqlConnection con = new SqlConnection("constr")) 
     { 
      using (SqlCommand cmd = new SqlCommand("select DISTINCT Name from Register where Name LIKE '%'[email protected]+'%'", con)) 
      { 
       con.Open(); 
       cmd.Parameters.AddWithValue("@SearchText", username); 
       SqlDataReader dr = cmd.ExecuteReader(); 
       while (dr.Read()) 
       { 
        result.Add(dr["Name"].ToString()); 
       } 
       return result; 
      } 
     }  


    } 

,當我在文本框中鍵入我沒有得到output.but當我刪除runat =「server」它的工作原理。我只需要在服務器上,以便我可以從服務器採取文本框的值

+0

插入HTML文本框的值作爲後面字符串名稱=的Request.Form [ 「名稱」]的代碼; – Wenson

+0

http://www.aspsnippets.com/Articles/Get-value-of-HTML-Input-TextBox-in-ASPNet-code-behind-using-C-and-VBNet.aspx – Wenson

+0

添加runat =「server」和檢查我的解決方案 – RajeeshMenoth

回答

0

當我刪除runat =「服務器」它的工作原理。

我認爲回發發生在您的代碼中。所以你必須重新綁定SearchText()函數。在您的代碼中添加以下javascript

嘗試此代碼:

var prm_lp = Sys.WebForms.PageRequestManager.getInstance(); 
//Re-bind for callbacks 
prm_lp.add_endRequest(function() { 
SearchText(); 
});