Similar to Baidu Smart Tips

Source: Internet
Author: User
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 ();
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.