2010-05-20 25 views
0

我正在构建一个HTTPHandler,它将提供用于jQuery Autocomplete的纯文本。我现在正在工作,除了当我插入文本的第一位时,它不会将我带到字母表的正确部分。优化LINQ查询与jQuery一起使用自动完成

例子:如果我进入Ne

我下拉回报

Nlabama
阿肯色州

通知书的自Ne从 “阿拉巴马”, “N” 和 “labama”

当我输入第三个字符New,那么jQuery返回结果的“N”部分。

我当前的代码看起来像这样

Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest 
    ''# the page ContentType is plain text 
    HttpContext.Current.Response.ContentType = "text/plain" 

    ''# store the querystring as a variable' 
    Dim qs As Nullable(Of Integer) = Integer.TryParse(HttpContext.Current.Request.QueryString("ID"), Nothing) 

    ''# use the RegionsDataContext' 
    Using RegionDC As New DAL.RegionsDataContext 

     ''# create a (q)uery variable' 
     Dim q As Object 

     ''# if the querystring PID is not blank' 
     ''# then we want to return results based on the PID' 
     If Not qs Is Nothing Then 
      ''# that fit within the Parent ID' 
      q = (From r In RegionDC.bt_Regions _ 
        Where r.PID = qs _ 
        Select r.Region).ToArray 

      ''# now we loop through the array' 
      ''# and write out the ressults' 
      For Each item In q 
       HttpContext.Current.Response.Write(item & vbCrLf) 
      Next 

     End If 


    End Using 
End Sub 

所以我在哪里,现在是我偶然在自动填充方法“Part”部分,由此我只应返回包含内的信息的事实那个部分。

我的问题是,我将如何实现这个概念到我的HTTPHandler没有做一个新的SQLQuery每个字符的变化? IE:我在QueryString(“ID”)上执行SQL查询,然后在相同ID的每个后续加载中,我们只是过滤掉“部分”。

http://www.example.com/ReturnRegions.axd?ID=[someID]&Part=[string]

+0

此外,在我发布的文章中,这个人谈论了使用JSON,并且我真的不知道关于JSON的第一件事或者它如何对应这个问题。 – 2010-05-20 02:45:10

回答

0

我改变了我的jQuery库使用jQuery的UI,然后使用一个Singleton对象,以避免打在每个页面加载数据库建立我的IHttpHandler。 answer here似乎对我很好。

1

只要抓住从查询字符串part,并用它的Where子句中仅筛选与它启动,并有相应的ID的区域。

ToArray的是没有必要的

+0

谢谢,我试过了,但不幸的是我仍然一遍又一遍地敲击数据库。经过大量的挖掘,我找到了我正在寻找的东西。你的建议指出了我朝着正确的方向。 http://stackoverflow.com/questions/2931877/cant-get-jquery-autocomplete-to-work-with-external-json/2933668#2933668 – 2010-05-31 05:19:32