Use ASP. Net ajax to develop the text box automatic prompt Function

Source: Internet
Author: User

Try to use ASP. NET Ajax extensionsPageRank query of thinkpageAdded the text box auto-Prompt function. When you enter the website address in the text box, a drop-down menu is automatically displayed, showing the recently queried website and narrowing the scope according to the entered text. For example, when "W" is entered, all web sites starting with W are listed. When "www. F" is entered, only www.felixwoo.com is displayed. When you click a URL in the drop-down menu, the content in the text box is automatically completed. The whole process is similar to that in IE's address bar. Display Effect

This function is implemented through ASP. NET Ajax autocompleteextender. You only need a few lines of code.
First, add the ASP: autocompleteextender tag on the page. Do not forget to have the ASP: scriptmanager statement before.

<Asp: scriptmanager id = "scriptmanager" runat = "server"/>
<Asp: autocompleteextender id = "autocompleteextender1" targetcontrolid = "searchtext"
Runat = "server" servicemethod = "getcompletionlist"
Servicepath = "~ /Searchautocomplete. asmx "minimumprefixlength =" 1 "/>

Targetcontrolid is the ID of the text box for entering the URL, servicepath is the WebService address for obtaining the website list, And servicemethod is the specific method in that WebService, minimumprefixlength = 1 indicates that the prompt starts when a character is entered.

In searchautocomplete. asmx, you need to return the recently queried website from the database, and filter out the website starting with prefixtext through the prefixtext parameter. In this way, you can implement the step-by-step prompt function. The searchautocomplete. asmx code is as follows:

[Webmethod]
Public String [] getcompletionlist (string prefixtext, int count)
{
List <string> List = dataprovider. geturllist ();
Foreach (string s in List)
{
If (S. startswith (prefixtext ))
{
List. Add (s );
}
}
Return list. toarray ();
}

Here, list <string> list is a declared string model, which is a new feature in. NET 2.0, avoiding the performance consumption caused by the original use of arraylist. Dataprovider. geturllist () returns the list of all websites from the database. The returned type is also list <string>. Other codes are easy to understand.

Just a few lines of code can be used to implement such interesting and practical functions. It cannot be said that ASP. NET Ajax has done too much for us.

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.