views:

44

answers:

1

Hi,

imagine i have one textbox (with autocomplete 1.1, not the latest UI version) and a checkbox. The textbox triggers an asp.net .ashx page that will execute a stored procedure at the sql-server and return the results.

It's all working, but i want to add another feature. When the checkbox is checked i want stored_procedure_1 to be executed. If the checkbox is unchecked, stored_procedure_2 must be executed. The checkbox is unchecked by default.

My question: How do i tell the ashx page if the checkbox is checked or not..? By default the autocomplete will trigger something like: autocompletedata.ashx?q=myname and that will execute stored_procedure_2, but when the checkbox is checked it must trigger autocompletedata.ashx?q=myname&mycheckbox=begin so that stored_procedure_1 will be executed.

Do i have to add some jQuery code to pass the checked checkbox? I am totally lost..

Thanks in advance.

Form elements:

<input id="search_employee" type="text" class="employee" />
<input type="checkbox" name="mycheckbox" value="begin" />

jQuery:

 $().ready(function() {

        $("#search_employee").autocomplete("includes/AutocompleteData.ashx", {
            minChars: 3,
            max: 15,
            selectFirst: false,
            scrollHeight: 300,
            formatItem: function(data, i, n, value) {
            if (value.split("_")[3]!== null)
            {
            return "<img style = 'width:40px;height:53px;float:left; margin:2px 5px 2px 0' src='/pictures/thumbs/"
            + value.split("_")[3] + "'/> " + value.split("_")[0] + "<br /><span class=smallname>" + value.split("_")[2] + "<br/>" + value.split("_")[4] + "</span>";
        } 
    },
            formatResult: function(data, value) {
                return value.split("_")[0];
            }
        });

Part of autocompletedata.ashx :

Dim searchname As String = context.Request.QueryString("q")
Dim searchstart as string = context.Request.QueryString("mycheckbox")
Dim searchsql as string

if searchstart = "begin" then
   searchsql = "stored_procedure_1"
else
   searchsql = "stored_procedure_2"
end if

Dim conn As SqlConnection = New SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("MyConn").ConnectionString
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = searchsql.ToString
cmd.Parameters.AddWithValue("@SearchText", searchname)         
+1  A: 

You can use .setOptions to set additional parameters:

ac.setOptions({ params: { paramOne:'somevalue', somethingelse:'foobar' } });

That is the programmatic way, but in your case:

$("#search_employee").autocomplete("includes/AutocompleteData.ashx", {
   params: { flag:'true' }, // just grab the checked attr beforehand
     ...//rest of your AC stuff

That will perform the following HTTP GET:

GET: includes/AutocompleteData.ashx?flag=true

Allowing you to access it via the Request.QueryString collection in the ASHX.

EDIT

My bad - i was thinking another version of AC.

I think the param is called "extraParams":

$("#search_employee").autocomplete("includes/AutocompleteData.ashx", {
   extraParams: { flag:'true' }

Try them both, one will/should work. :)

HTH.

RPM1984
Thanks for helping me in the right direction :)When i add "extraParams: {'mycheckbox':'begin'}" it will indeed call stored_procedure_1. That's one step further.But it should only add those extraparameter when the checkbox is selected (or otherwise return false). How (and where) do i set the value of the checkbox dynamically?
juno-2
What do you mean "return false"? By the looks of your code, if the CB is checked, you want to call SP1, otherwise SP2 - isn't that right? You won't be able to pass "begin" or null, keep it simple - just pass the checked attribute directly to the ASHX: `extraParams: { flag: $("#idOfYourCheckbox").attr("checked") }`.
RPM1984
Thanks, that did it!
juno-2
Great, glad it worked. :) BTW - the new AC is waaaaay better (i'm using it at the moment). You get full control of the AJAX call, and you can apply different themes. Just thought i'd mention that. :)
RPM1984