views:

2422

answers:

3

how do I pass additional information to the service method returning the collection of items? I'll attempt to explain what I mean, I have 2 text boxes on a form, I need to fill out names, based of a specific account id in a database. so, I need to pass an integer to the getNamesForDropDown method. I couldn't figure out what to do, so I did the wrong thing, and used the CompletionSetCount to actually pass the information I needed:

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] getNamesForDropDown(string prefixText, int count) 
{
   String sql = "Select fldName From idAccountReps Where idAccount = " + count.ToString();
   //... rest of the method removed, this should be enough code to understand
   //... the evil wrongness I did. 
}

in my front side aspx file, i set the CompletionSetCount based off the Account id the user is currently viewing on that page.

<ajaxtk:AutoCompleteExtender 
    runat="server" 
    ID="AC1" 
    TargetControlID="txtAccName"
    ServiceMethod="getNamesForDropDown"
    ServicePath="AccountInfo.asmx"
    MinimumPrefixLength="1" 
    EnableCaching="true"
    CompletionSetCount='<%# Eval("idAccount") %>'
/>

So, that's definitely a wrong way... what would be the right way?

+1  A: 

If you like you can use a separator with the prefixText. So, you can pass "1:bcd" and on the service end you can split the two items:

string[] arguments = prefixText.Split(':'); 
int id = Int32.Parse(arguments[0]);
string text = arguments[1];
azamsharp
how can I control what prefix text gets sent to the method?
stephenbayer
Ohh I see!! Yes you are right. The control instantly focus on the ServiceMethod without stopping in between. I will need to check on this later!
azamsharp
+2  A: 

Holy smoke, I think this is what I need, I swear I never saw this option before I started programming this. Is this a new property to the autocompleteextender?

Excerpt from Documentation:

ContextKey - User/page specific context provided to an optional overload of the web method described by ServiceMethod/ServicePath. If the context key is used, it should have the same signature with an additional parameter named contextKey of type string:

[System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] public string[] GetCompletionList( string prefixText, int count, string contextKey) { ... }

Note that you can replace "GetCompletionList" with a name of your choice, but the return type and parameter name and type must exactly match, including case.

Edit: It doesn't matter if it is new or not, or whether i just completely overlooked it. It works, and I'm happy. It took me about 10 minutes from being confused to figuring out my own answer.

stephenbayer
+2  A: 

azam has the right idea- but the signature of the autocomplete method can also have a third parameter:

public string[] yourmethod(string prefixText, int count, string contextKey)

you can Split up the results of the contextKey string using Azam's method- but this way you do not have to worry about sanatizing the user's input of the .Split()'s delimiter (:)

Thanks for the correction!
azamsharp