jQuery自动完成和ASP.NET

dju*_*uth 50 asp.net subsonic jquery autocomplete

我在这个网站和网页上搜索了一个使用jQuery和ASP.NET自动完成的简单例子.我想用web服务公开自动完成所使用的数据(接下来可能会这样做).与此同时,我得到了这个工作,但似乎有点hacky ......

在我的页面中,我有一个文本框:

<input id="txtSearch" type="text" />
Run Code Online (Sandbox Code Playgroud)

我正在使用jQuery自动完成,根据他们的示例进行设置:

<link rel="stylesheet" href="js/jquery.autocomplete.css" type="text/css" />
<script type="text/javascript" src="js/jquery.bgiframe.js"></script>
<script type="text/javascript" src="js/jquery.dimensions.pack.js"></script>
<script type="text/javascript" src="js/jquery.autocomplete.js"></script>
Run Code Online (Sandbox Code Playgroud)

这是它开始变得hacky ...我称之为页面而不是web服务:

  <script type="text/javascript">
    $(document).ready(function(){
        $("#txtSearch").autocomplete('autocompletetagdata.aspx');
    });      
  </script>
Run Code Online (Sandbox Code Playgroud)

在页面中我删除了所有的html并且只有这个(否则,各种HTML位显示在自动完成下拉列表中):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="autocompletetagdata.aspx.cs" Inherits="autocompletetagdata" %>
Run Code Online (Sandbox Code Playgroud)

在我的autocompletetagdata.aspx中,我使用SubSonic从数据库中查询,格式化和返回数据(每行一个数据项):

protected void Page_Load(object sender, EventArgs e)
{
    // Note the query strings passed by jquery autocomplete:
    //QueryString: {q=a&limit=150&timestamp=1227198175320}

    LookupTagCollection tags = Select.AllColumnsFrom<LookupTag>()
        .Top(Request.QueryString["limit"])
        .Where(LookupTag.Columns.TagDescription).Like(Request.QueryString["q"] + "%")
        .OrderAsc(LookupTag.Columns.TagDescription)
        .ExecuteAsCollection<LookupTagCollection>();

    StringBuilder sb = new StringBuilder();

    foreach (LookupTag tag in tags)
    {
        sb.Append(tag.TagDescription).Append("\n");
    }

    Response.Write(sb.ToString());
}
Run Code Online (Sandbox Code Playgroud)

如果你不进行LIKE查询,那么它会返回包含你输入的字符匹配的所有内容 - 例如,输入"a"将包括"Ask"和"Answer"以及"March"和"兆." 我只是想让它以匹配开始.

无论如何,它的工作原理很容易设置,但还有更好的方法吗?

bdu*_*kes 32

我刚刚实现了自动完成功能,它看起来非常相似.我使用的是ashx(Generic Handler)而不是aspx,但它背后的代码基本上是相同的代码.

使用ashx,它看起来像这样:

<script type="text/javascript">
  $(document).ready(function(){
      $("#txtSearch").autocomplete('autocompletetagdata.ashx');
  });      
</script>

[WebService(Namespace = "http://www.yoursite.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AutocompleteTagData : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        // Note the query strings passed by jquery autocomplete:
        //QueryString: {q=a&limit=150&timestamp=1227198175320}

        LookupTagCollection tags = Select.AllColumnsFrom<LookupTag>()
            .Top(context.Request.QueryString["limit"])
            .Where(LookupTag.Columns.TagDescription).Like(context.Request.QueryString["q"] + "%")
            .OrderAsc(LookupTag.Columns.TagDescription)
            .ExecuteAsCollection<LookupTagCollection>();

        foreach (LookupTag tag in tags)
        {
            context.Response.Write(tag.TagDescription + Environment.NewLine);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Zac*_*ary 10

我刚刚在另一个问题上发布了这个问题,但您可以覆盖jQuery自动完成插件上的解析函数,使其支持任何输出.

例:

    $("#<%= TextBox1.ClientID %>").autocomplete("/Demo/WebSvc.asmx/SuggestCustomers", {
        parse: function(data) {
            var parsed = [];

            $(data).find("string").each(function() {
                parsed[parsed.length] = {
                    data: [$(this).text()],
                    value: $(this).text(),
                    result: [$(this).text()]
                };
            });
            return parsed;
        },
        dataType: "xml"
    });
Run Code Online (Sandbox Code Playgroud)

所有这一切都需要一个XML字符串数组...非常容易...如果您使用SubSonic,您应该查看RESTHandler(这是一个隐藏的GEM !!!),它支持所有对象的基本查询,并且可以返回JSON/XML.这是一个使用它的示例查询...

/Demo/services/Customers/list.xml?CustomerName=JOHN

如果将list.xml更改为list.json,它会将结果更改为JSON.上面的请求将返回一个强类型的"Customer"实体.您可以更改参数以支持LIKE,NOT LIKE等...非常强大且所有管道都已完成...

这是一个视频:http: //subsonicproject.com/tips-and-tricks/webcast-using-subsonic-s-rest-handler/


tva*_*son 6

Web服务或WCF服务将为您提供更好的界面.两者都可以设置为执行Json序列化.

因为我正在写一个WCF类(我正在休息,真的!),我将草拟WCF方法.

[OperationContract]
[WebInvoke(RequestFormat=WebMessageFormat.Json,
           ResponseFormat=WebMessageFormat.Json)]
public LookupTagCollection LookupTags( int limit, string q )
{
     return Select.AllColumnsFrom<LookupTag>()
                  .Top(limit)
                  .Where(LookupTag.Columns.TagDescription)
                  .Like(q+ "%")
                  .OrderAs(LookupTag.Columns.TagDescription)
                  .ExecuteAsCollection<LookupTagCollection>();    
}
Run Code Online (Sandbox Code Playgroud)

LookupTagCollection需要是Serializable.