2013-02-19 30 views
2

我想列出我的文本框中的機場名稱從我的數據庫中獲取例如,當我在文本框中鍵入拉斯維加斯。文本框應該給lasvegas。我想要數據庫驅動的自動完成文本框使用javascript和數據庫在c#

Default.aspx.cs:

public string listFilter = null; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    listFilter = BindName(); 
} 

private string BindName() 
{ 
    try 
    { 
     SqlConnection con = new SqlConnection(@"Data Source=172.16.10.170;Initial Catalog=cbtsv;User ID=cbtsv;Password=cbtsvpass;"); 
     con.Open(); 
     DataTable ds = new DataTable(); 
     using (SqlCommand cmd = con.CreateCommand()) 
     { 
      SqlCommand com = new SqlCommand("select SearchKey from DTAirportCity where SearchKey like '%TextBox1.Text%'", con); 
      SqlDataAdapter sda = new SqlDataAdapter(com); 

      sda.Fill(ds); 

     } 

     StringBuilder output = new StringBuilder(); 
     output.Append("["); 
     for (int i = 0; i < ds.Rows.Count; ++i) 
     { 
      output.Append("\"" + ds.Rows[i]["SearchKey"].ToString() + "\""); 

      if (i != (ds.Rows.Count - 1)) 
      { 
       output.Append(","); 
      } 
     } 
     output.Append("];"); 

     return output.ToString(); 
     con.Close(); 
    } 
    catch (Exception) 
    { 

     throw; 
    } 
} 

Default.aspx的:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> 
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> 
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> 
<title></title> 
<script type="text/javascript"> 
    function LoadList() { 
     var dt=null; 
    dt = <%=listFilter %> 
    } 
</script> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 

<asp:TextBox ID="TextBox1" runat="server" OnLoad="LoadList()"></asp:TextBox>  
    <br /> 

</div> 
</form> 

問題: - LoadList()函數是行不通的。

+0

使用jquery自動完成 – 2013-02-19 11:39:05

+0

如何實現。 – 2013-02-19 11:41:18

+0

你的意思是你有一個數據庫,你想使用該數據庫的文本框自動完成功能 – amitesh 2013-02-19 11:42:05

回答

-1

嘗試下面的代碼,這是工作示例:

$(document).ready(function() { 
     $('[ID$=txtPatientLastname]').live('keyup.autocomplete', function() { 

      $(this).autocomplete({ 
       source: function (request, response) { 
        $.ajax({ 
         url: '<%=ResolveUrl("~/Resources/WebService.asmx/GetPatientLastName") %>', 
         data: "{ 'prefix': '" + request.term + "'}", 
         dataType: "json", 
         type: "POST", 
         contentType: "application/json; charset=utf-8", 
         success: function (data) { 
          response($.map(data.d, function (item) { 
           return { 
            label: item.split('-')[0], 
            val: item.split('-')[1] 
           } 
          })) 
         }, 
         error: function (response) { 
          alert(response.responseText); 
         }, 
         failure: function (response) { 
          alert(response.responseText); 
         } 
        }); 
       }, 
       select: function (e, i) { 
       }, 
       minLength: 1 
      }); 
     }); 
}); 

而且你可以創建一個WebMethod,或者您可以使用Web服務,因爲我已經做了

WEBSERVICES代碼:

[WebMethod(EnableSession = true)] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string[] GetPatientLastName(string prefix) 
{ 
    List<string> customers = new List<string>(); 
    using (SqlConnection conn = new SqlConnection()) 
    { 
     string connectionstring = CCMMUtility.GetCacheForWholeApplication(); 
     conn.ConnectionString = connectionstring; 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.CommandText = "select distinct top(10) PatientLastname from tblMessage where " + 
      "PatientLastname like '%'+ @SearchText + '%' order by PatientLastname"; 
      cmd.Parameters.AddWithValue("@SearchText", prefix); 
      cmd.Connection = conn; 
      conn.Open(); 
      using (SqlDataReader sdr = cmd.ExecuteReader()) 
      { 
       while (sdr.Read()) 
       { 
        customers.Add(string.Format("{0}", sdr["PatientLastname"])); 
       } 
      } 
      conn.Close(); 
     } 
     return customers.ToArray(); 
    } 
} 

希望這會對你有用。

+1

不要複製粘貼代碼男人你複製代碼從http://www.aspdotnet-suresh.com/2012/08/using-jquery-autocomplete-with-aspnet.html鏈接並粘貼在這裏 – amitesh 2013-02-19 11:45:42

+0

@ amitesh親愛的我已經實現了我從那裏粘貼代碼的例子.. – 2013-02-19 11:47:04

相關問題