After one afternoon, I finally finished the result (similar to the Baidu smart prompt function). Because I was only familiar with AJAX technology, it took some time to do this, thick and thin hair .... Let's just look at the code first.
Front-end code:
<Asp: ScriptManager ID = "ScriptManager1" runat = "server"/>
<Div>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<C0: AutoCompleteExtender ID = "AutoCompleteExtender1" runat = "server" TargetControlID = "TextBox1"
MinimumPrefixLength = "1" ServiceMethod = "GetProductName" ServicePath = "WebService. asmx">
</PC3: AutoCompleteExtender>
</Div>
Background WebService code:
Using System;
Using System. Web;
Using System. Collections;
Using System. Web. Services;
Using System. Web. Services. Protocols;
Using System. Data;
Using System. Data. SqlClient;
Using System. Web. Script. Services; // key Assembly reference
Using System. Collections. Generic; // key Assembly reference
/// <Summary>
/// Summary of WebService
/// </Summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
[ScriptService ()] // be sure to add
Public class WebService: System. Web. Services. WebService {
Public WebService (){
// If you use the designed component, uncomment the following line
// InitializeComponent ();
}
[WebMethod]
[ScriptMethod]
Public string [] GetProductName (string prefixText, int count)
{
List <string> suggestions = new List <string> (); // declare a generic set
SqlConnection con = new SqlConnection ("server =.; database = NorthWind; uid = sa; pwd = ;");
Con. Open ();
SqlCommand com = new SqlCommand ("select distinct productname from Products where productname like @ prefixname order by productname", con );
Com. Parameters. Add ("@ prefixname", SqlDbType. NVarChar). Value = prefixText + "% ";
SqlDataReader sdr = com. ExecuteReader ();
While (sdr. Read ())
{
Suggestions. Add (sdr. GetString (0 ));
}
Sdr. close ();
Con. close ();
Return suggestions. ToArray ();
}
}