C# 数据库中的自动完成文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8926223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
autocomplete textbox from the database
提问by asifa
I need my textbox to autocomplete when the user types. The value should come from the database. I am using the textchange property of the textbox.
当用户键入时,我需要我的文本框自动完成。该值应该来自数据库。我正在使用文本框的 textchange 属性。
protected void autocomplete(object sender, EventArgs e)
{
string query = "Select area,codes from tbl_pincode";
SqlConnection conn = new SqlConnection("Data Source=win2008-2;Initial Catalog=h1tm11;User ID=sa;Password=#1cub3123*;Persist Security Info=True;");
SqlCommand com = new SqlCommand(query, conn);
conn.Open();
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
zipcode.Text = dr.GetValue(0).ToString();
}
conn.Close();
}
But i m not getting the desired result. Any ideas how to go about it?
但我没有得到想要的结果。任何想法如何去做?
采纳答案by Fabio
You can use jQuery UI for autocomplete: http://www.dotnetcurry.com/ShowArticle.aspx?ID=515
您可以使用 jQuery UI 进行自动完成:http: //www.dotnetcurry.com/ShowArticle.aspx?ID= 515
Another option for ASP.NET autocomplete is AjaxControlToolkit: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx
ASP.NET 自动完成的另一个选项是 AjaxControlToolkit:http: //www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx
回答by Magrangs
Have you looked at using the jquery ui autocomplete component? You can hook this up to a remote datasource
您是否考虑过使用 jquery ui 自动完成组件?您可以将其连接到远程数据源
回答by Strillo
- Never put your connection strings on ANY forum
- Use AJAX otherwise you'll have to postback the page every time the user types a character. JQuery & JQueryUIprovide easy support for autocomplete features.
- use Telerik RadCombo(but you need to buy a license)
- 永远不要把你的连接字符串放在任何论坛上
- 使用 AJAX,否则每次用户键入字符时都必须回发页面。JQuery 和JQueryUI为自动完成功能提供了简单的支持。
- 使用Telerik RadCombo(但您需要购买许可证)
EDIT:
编辑:
If you choose to use the JQueryUI autocomplete, I'd start from the remote JSONPexample.
You can point the urlproperty of the ajaxcall inside the autocomplete's sourcefunction to a WebMethodin your page. This will receive an object containing the filter (datain the example) and return the required values from your database in JSON format (e.g. see thisexample)
如果您选择使用 JQueryUI 自动完成功能,我将从远程 JSONP示例开始。您可以将自动完成功能内url的ajax调用属性指向页面中source的WebMethod。这将接收一个包含过滤器的对象(data在示例中)并以 JSON 格式从您的数据库返回所需的值(例如,请参阅此示例)
回答by ?iamond ?eeze?
You are selecting all rows from the tbl_pincodetable, not just those that match the character(s) the user has input. You are then assigning the value from the areacolumn for each row to zipcode.Text.
您正在选择tbl_pincode表中的所有行,而不仅仅是那些与用户输入的字符匹配的行。然后,您将area每一行的列中的值分配给zipcode.Text。
Maybe try:
也许尝试:
protected void autocomplete(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(zipcode.Text))
return;
string query = @"
SELECT area FROM tbl_pincode WHERE area LIKE @Value
UNION
SELECT codes FROM tbl_pincode WHERE codes LIKE @Value";
SqlConnection conn = null;
SqlCommand com = null;
SqlDataReader dr = null;
try
{
conn = new SqlConnection("Data Source=win2008-2;Initial Catalog=h1tm11;User ID=sa;Password=#1cub3123*;Persist Security Info=True;");
com = new SqlCommand(query, conn);
string value = string.Format("{0}%", zipcode.Text);
com.Parameters.AddWithValue("@Value", value);
conn.Open();
dr = com.ExecuteReader();
if (dr.Read())
{
zipcode.Text = dr.GetValue(0).ToString();
}
}
finally
{
if (conn != null) conn.Dispose();
if (dr != null) dr.Dispose();
if (com != null) com.Dispose();
}
}
The SQL selects area and codes that start with the character(s) the user has input so far. which is what I think based on your comments to other answers. I've also change the whileloop to and ifblock as I can see why you would want to assign and then re-assign values to the zipcode.Textproperty (I could understand if you were appending to a List control).
SQL 选择以用户迄今为止输入的字符开头的区域和代码。这是我根据您对其他答案的评论所认为的。我还将while循环更改为和if阻止,因为我可以理解为什么您要为该zipcode.Text属性分配然后重新分配值(如果您附加到 List 控件,我可以理解)。

