asp教程.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 /><br />
<asp:label
id="lblselectedproducttitle"
runat="server" />
</div>
</form>
</body>
</html>
方法二
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 /><br />
<asp:label
id="lblselectedfilename"
runat="server" />
</div>
</form>
</body>
</html>