Comment faire un TextBox autocomplete en C# qui se lie à une source de données?Comment faire un TextBox autocomplete dans ASP.NET?
Répondre
Vous pouvez utiliser jQuery Autocomplete ou ASP.NET AJAX Toolkit Autocomplete
J'utilise AutoComplete de boîte à outils ajaxcontrol
Je ne recommanderais pas (personnellement) cette solution. En 2010, quand cela a été répondu peut-être. Maintenant, ne l'utilisez pas. Il existe de meilleurs moyens de le faire en utilisant JavaScript, jquery, etc. – Liam
1 Installez AjaxControl Toolkit facilement Nugget
PM> Install-Package AjaxControlToolkit
2-puis dans le balisage
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:TextBox ID="txtMovie" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" TargetControlID="txtMovie"
runat="server" />
3- dans le code-behind: à obtenir les suggestions
[System.Web.Services.WebMethodAttribute(),System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey) {
// Create array of movies
string[] movies = {"Star Wars", "Star Trek", "Superman", "Memento", "Shrek", "Shrek II"};
// Return matching movies
return (from m in movies where m.StartsWith(prefixText,StringComparison.CurrentCultureIgnoreCase) select m).Take(count).ToArray();
}
source: http://www.asp.net/ajaxlibrary/act_autocomplete_simple.ashx
Essayez ceci: page .aspx
<td>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:AutoCompleteExtender ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
CompletionInterval="10" EnableCaching="false" CompletionSetCount="1" TargetControlID="TextBox1"
ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">
</asp:AutoCompleteExtender>
Maintenant Pour la base de données auto peupler:
public static List<string> GetCompletionList(string prefixText, int count)
{
return AutoFillProducts(prefixText);
}
private static List<string> AutoFillProducts(string prefixText)
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
using (SqlCommand com = new SqlCommand())
{
com.CommandText = "select ProductName from ProdcutMaster where " + "ProductName like @Search + '%'";
com.Parameters.AddWithValue("@Search", prefixText);
com.Connection = con;
con.Open();
List<string> countryNames = new List<string>();
using (SqlDataReader sdr = com.ExecuteReader())
{
while (sdr.Read())
{
countryNames.Add(sdr["ProductName"].ToString());
}
}
con.Close();
return countryNames;
}
}
}
Maintenant: créez une procédure stockée qui récupère les détails du produit en fonction du produit sélectionné dans la zone de texte Auto Complete.
Create Procedure GetProductDet
(
@ProductName varchar(50)
)
as
begin
Select BrandName,warranty,Price from ProdcutMaster where [email protected]
End
Créer un nom de fonction pour obtenir les détails du produit ::
private void GetProductMasterDet(string ProductName)
{
connection();
com = new SqlCommand("GetProductDet", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@ProductName", ProductName);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds=new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
con.Close();
//Binding TextBox From dataTable
txtbrandName.Text =dt.Rows[0]["BrandName"].ToString();
txtwarranty.Text = dt.Rows[0]["warranty"].ToString();
txtPrice.Text = dt.Rows[0]["Price"].ToString();
}
après la baisse automatique doit être vrai
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
Maintenant, appelez simplement cette fonction
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
//calling method and Passing Values
GetProductMasterDet(TextBox1.Text);
}
- 1. AutoComplete TextBox Control
- 2. suggestion autocomplete textbox
- 3. Autocomplete ajax textbox pour l'application web iphone?
- 4. Comment faire pour faire pivoter un TextBox dans Microsoft Report?
- 5. Comment faire Textbox Envoyer
- 6. Comment faire un feu ASP.NET TextBox c'est feu d'événement onTextChanged dans un UpdatePanel AJAX?
- 7. Comment puis-je utiliser Ajax Toolkit Extender pour faire AutoComplete textBox
- 8. Comment faire défilement TextBox dans WPF
- 9. asp.net MVC: désactiver un TextBox
- 10. JQUERY et autocomplete asp.NET
- 11. comment afficher dynamiquement dans asp.net textbox
- 12. ASP.NET validation XHTML Strict & autocomplete = off
- 13. JQuery autocomplete + Asp.net MVC
- 14. ASP.net MVC AutoComplete Erroring?
- 15. faire textbox extensible
- 16. Grails: Comment faire un g: autocomplete de champ de texte?
- 17. Association d'une paire valeur/clé à l'extension autoComplete de textBox
- 18. ASP.NET MVC jQuery Autocomplete HtmlHelper
- 19. .NET AutoComplete DropDownList ou TextBox sur le navigateur Web mobile?
- 20. asp.net-mvc validation textbox
- 21. Comment faire un TextBox non interactif en C#
- 22. Comment faire Entrée sur un TextBox agir comme bouton TAB
- 23. jQuery autocomplete dans les formulaires Web ASP.NET?
- 24. Validation TextBox ASP.Net
- 25. Filtre TextBox ASP.NET
- 26. Comment faire un menu dans asp.net
- 27. Label, TextBox, Asp.net
- 28. ASP.Net textbox onblur événement
- 29. Comment faire Input TextBox avec Style Mathon?
- 30. Comment simuler coller dans un TextBox WPF?
I Je vais entrer ici, j'ai eu de bonnes expériences avec jQuery Autocomlete. Une documentation peut être trouvée à http://docs.jquery.com/Plugins/autocomplete. –