asp.net AutoComplete Control

來源:互聯網
上載者:User

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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.