Asp.net AutoComplete Control

Source: Internet
Author: User

Asp tutorial. net autocomplete control
<% @ Page language = "c #" %>
<% @ Register tagprefix = "ajax" namespace = "ajaxcontroltoolkit"
Assembly = "ajaxcontroltoolkit" %>
<% @ Import namespace = "system. linq" %>
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd>
<Script runat = "server">
[System. web. services. webmethod]
Public static string [] getsuggestions (string prefixtext, int count)
{
Mydatabasedatacontext db = new mydatabasedatacontext ();
Return db. products
. Where (m => m. title. startswith (prefixtext ))
. Orderby (m => m. title)
. Select (m => m. title)
. Take (count)
. Toarray ();
}

Protected void btnsubmit_click (object sender, eventargs e)
{
Lblselectedproducttitle. text = txtproducttitle. text;
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> autocomplete page method </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>

<Asp: scriptmanager id = "sm1" runat = "server"/>
<Asp: label
Id = "lblproducttitle"
Text = "product :"
Associatedcontrolid = "txtproducttitle"
Runat = "server"/>
<Asp: textbox
Id = "txtproducttitle"
Autocomplete = "off"
Runat = "server"/>
<Ajax: autocompleteextender
Id = "ace1"
Targetcontrolid = "txtproducttitle"
Servicemethod = "getsuggestions"
Minimumprefixlength = "1"
Runat = "server"/>
<Asp: button
Id = "btnsubmit"
Text = "submit"
Onclick = "btnsubmit_click"
Runat = "server"/>

<Br/>

<Asp: label
Id = "lblselectedproducttitle"
Runat = "server"/>

</Div>
</Form>
</Body>
</Html>

Method 2

File: fileservice. asmx

<% @ Webservice language = "c #" class = "fileservice" %>
Using system;
Using system. web;
Using system. web. services;
Using system. web. services. protocols;
Using system. io;
Using system. linq;

[Webservice (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[System. web. script. services. scriptservice]
Public class fileservice: system. web. services. webservice {

[Webmethod]
Public string [] getsuggestions (string prefixtext, int count)
{
Directoryinfo dir = new directoryinfo ("c: windows ");
Return dir
. Getfiles ()
. Where (f => f. name. startswith (prefixtext ))
. Select (f => f. name)
. Toarray ();
}
}


File: autocompletewebservice. aspx

<% @ Page language = "c #" %>
<% @ Register tagprefix = "ajax" namespace = "ajaxcontroltoolkit"
Assembly = "ajaxcontroltoolkit" %>
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd>
<Script runat = "server">
Protected void btnsubmit_click (object sender, eventargs e)
{
Lblselectedfilename. text = txtfilename. text;
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> show autocomplete web service </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>

<Asp: scriptmanager id = "sm1" runat = "server"/>

<Asp: label
Id = "lblfilename"
Text = "file name :"
Associatedcontrolid = "txtfilename"
Runat = "server"/>
<Asp: textbox
Id = "txtfilename"
Autocomplete = "off"
Runat = "server"/>
<Ajax: autocompleteextender
Id = "ace1"
Targetcontrolid = "txtfilename"
Servicemethod = "getsuggestions"
Servicepath = "~ /Fileservice. asmx"
Minimumprefixlength = "1"
Runat = "server"/>
<Asp: button
Id = "btnsubmit"
Text = "submit"
Onclick = "btnsubmit_click"
Runat = "server"/>

<Br/>

<Asp: label
Id = "lblselectedfilename"
Runat = "server"/>
</Div>
</Form>
</Body>
</Html>

Related Article

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.