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>