views:

137

answers:

2

I'm working on building an HTTPHandler that will serve up plain text for use with jQuery Autocomplete. I have it working now except for when I insert the first bit of text it does not take me to the right portion of the alphabet.

Example: If I enter Ne

my drop down returns

Nlabama
Arkansas

Notice the "N" from Ne and the "labama" from "Alabama"

As I type the third character New, then the jQuery returns the "N" section of the results.

My current code looks like this

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

So where I'm at now is the fact that I stumbled on the "Part" portion of the Autocomplete method whereby I should only return information that is contained within the Part.

My question is, how would I implement this concept into my HTTPHandler without doing a fresh SQLQuery on every character change? IE: I do the SQL Query on the QueryString("ID"), and then on every subsequent load of the same ID, we just filter down the "Part".

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

+1  A: 

Just grab the part from the querystring and use it in the Where clause to filter only Regions that start with it AND have the appropriate ID.

ToArray is not necessary

Hightechrider
Thanks, I tried that but unfortunately I was still hitting the database over and over. I figured out what I was looking for after a lot of digging. Your suggestion pointed me in the right direction tho. http://stackoverflow.com/questions/2931877/cant-get-jquery-autocomplete-to-work-with-external-json/2933668#2933668
rockinthesixstring
A: 

I changed my jQuery library to use the jQuery-UI and then built my iHttpHandler using a Singleton object in order to avoid hitting the database on every page load. The answer here seems to be working well for me.

rockinthesixstring