2010-10-04 98 views

Respuesta

4

Yo uso de ajaxcontrol conjunto de herramientas AutoComplete

+4

No recomendaría (personalmente) esta solución. En 2010, cuando esto fue respondido, tal vez. Ahora, no uses esto. Hay mejores formas de hacerlo usando JavaScript, jquery, etc. – Liam

1

1-Instalar AjaxControl Toolkit fácilmente por pepita

PM> Install-Package AjaxControlToolkit 

2-a continuación, en el marcado

<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- en el código subyacente: a obtener las sugerencias

[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(); 
    } 

fuente: http://www.asp.net/ajaxlibrary/act_autocomplete_simple.ashx

3

Prueba esto: página .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> 

Ahora automático Para poblar la base de datos:

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; 
      } 
     } 
    } 

Ahora: cree un Procedimiento almacenado que recupere los detalles del Producto según el producto seleccionado del Cuadro de texto Completar automáticamente.

Create Procedure GetProductDet 
( 
@ProductName varchar(50)  
) 
as 
begin 
Select BrandName,warranty,Price from ProdcutMaster where [email protected] 
End 

Crear un nombre de función para obtener los detalles del producto ::

volver después
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(); 
    } 

de autos debe ser verdad

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox> 

ahora, sólo llamar a esta función

protected void TextBox1_TextChanged(object sender, EventArgs e) 
    { 
     //calling method and Passing Values 
     GetProductMasterDet(TextBox1.Text); 
    } 
Cuestiones relacionadas