ASP.NET中的AutoCompleteExtender附加信息

时间:2008-10-09 19:45:36

标签: asp.net ajaxcontroltoolkit autocompleteextender

如何将其他信息传递给返回项目集合的服务方法?我将尝试解释我的意思,我在表单上有2个文本框,我需要根据数据库中的特定帐户ID填写名称。所以,我需要将一个整数传递给getNamesForDropDown方法。我无法弄清楚要做什么,所以我做错了,并使用CompletionSetCount实际传递了我需要的信息:

[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. 
}

在我的正面aspx文件中,我根据用户当前在该页面上查看的帐户ID设置了CompletionSetCount。

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

所以,这绝对是一种错误的方式......什么是正确的方式?

4 个答案:

答案 0 :(得分:5)

azam有正确的想法 - 但自动完成方法的签名也可以有第三个参数:

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

你可以使用Azam的方法拆分contextKey字符串的结果 - 但是这样你就不必担心用户输入.Split()的分隔符(:)

答案 1 :(得分:3)

神圣的烟雾,我想这就是我需要的东西,我发誓我在开始编程之前从未见过这个选项。这是autocompleteextender的新属性吗?

Excerpt from Documentation

ContextKey - 提供给ServiceMethod / ServicePath描述的Web方法的可选重载的用户/页面特定上下文。如果使用了上下文密钥,它应该具有相同的签名,并带有一个名为contextKey的附加参数,类型为string:

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

请注意,您可以使用您选择的名称替换“GetCompletionList”,但返回类型和参数名称和类型必须完全匹配,包括大小写。

编辑:它是否是新的无关紧要,或者我是否完全忽略了它。它有效,我很高兴。我花了大约10分钟才弄清楚我自己的答案。

答案 2 :(得分:2)

如果您愿意,可以使用带有prefixText的分隔符。因此,您可以传递“1:bcd”,在服务端,您可以拆分这两个项目:

string[] arguments = prefixText.Split(':'); 
int id = Int32.Parse(arguments[0]);
string text = arguments[1]; 

答案 3 :(得分:1)

相关问题