Ajax自动完成扩展程序

时间:2010-05-25 17:52:40

标签: asp.net-ajax

我有一个ajax自动完成扩展程序。我想将隐藏字段的值传递给Web服务。我想将隐藏字段的值作为contextkey参数传递给Web服务。

2 个答案:

答案 0 :(得分:0)

我明白了。我已经发布了下面的代码,以防其他人有类似的问题。

<asp:Panel id ="divClientSearch" CssClass="clientSearch" runat="server" DefaultButton="btnSearch">

    <label>Enter Client Last Name/First Name/Full Name/SSN: </label>
     <asp:TextBox ID="txtSearch" runat="server" MaxLength="20" autocomplete="off"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" SkinID="smallButton" OnClick="BtnSearchClick"
        OnClientClick="return RedirectPage();" Text="Search" />
        <input type="hidden" id="searchClient" runat="server" />
        <asp:TextBox ID="searchClient22" runat="server" MaxLength="20" autocomplete="off" Visible="false"></asp:TextBox>
         <ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtSearch" BehaviorID="AutoCompleteBehavior" 
        ServiceMethod="GetClients" ServicePath="~/AjaxServices/TickerSearch.asmx"
        MinimumPrefixLength="1" CompletionInterval="1000" EnableCaching="true" CompletionSetCount="200"
        CompletionListCssClass="autocomplete_completionListElement menuList" CompletionListItemCssClass="autocomplete_listItem"
        CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" DelimiterCharacters=""
        ShowOnlyCurrentWordInCompletionListItem="true">
    </ajaxToolkit:AutoCompleteExtender>
</asp:Panel>


<script type="text/javascript">

    function pageLoad() {
        var autoComplete = $find('AutoCompleteBehavior');
        if (!autoComplete) return;
        var target = autoComplete.get_element();
        if (!target) return;

        var userContext = document.getElementById('<%=searchClient.ClientID %>').value;
        if (!userContext) return;

        // Dynamically assign the context and change the color when processing
        autoComplete.add_populating(function() {
        autoComplete.set_contextKey(userContext);

        });
    }
</script>

答案 1 :(得分:0)

如果$ find(BehaviorID)返回null,则可以这样做:

//连接到自动完成填充/填充事件

function pageLoad() {
    var autoComplete = getBehavior('AutoCompleteBehavior');
    if (!autoComplete) return;
    var target = autoComplete.get_element();
    if (!target) return;
    var userContext = $get('<%=this.ddlColumnName.ClientID %>');
    if (!userContext) return;

    // Dynamically assign the context and change the color when processing
    autoComplete.add_populating(function () {
        autoComplete.set_contextKey(userContext.value);
    });

    autoComplete.add_itemSelected(function () {
        _doPostBack('<%=this.ddlColumnName.ClientID %>', '');
    });
}

function getBehavior(name) {
    //If any Extender is placed in the DataBind Control, it's hard to define the BehaviorId and get the Client behavior. 
    //We can use the method to find all the correct type behaviors.
    var currentBehavior = null;
    var allBehaviors = Sys.Application.getComponents();
    for (var loopIndex = 0; loopIndex < allBehaviors.length; loopIndex++) {
        currentBehavior = allBehaviors[loopIndex];
        if ("get_name" in currentBehavior) {
            if (currentBehavior.get_name() == name) {
                // Now we get the ClientBehavior here: currentBehavior!
                return currentBehavior
            }
        }
    }
    return
}