开发者

How do you get the AutoCompleteExtender to submit when an item is clicked

I have a textbox and a button on my ASP.net form for executing a search. I have added an autocompleteextender from the AJAX toolkit to show suggestions whi开发者_运维知识库le the user is typing. This works fine, however what I want to happen is for the Click event of the button to fire when the user selects an item in the displayed list of suggestions. Anyone any idea how to do this?


Since the item selected event is going to fire a client side JavaScript event, I typically add the following code to my OnClientItemSelected event method:

<script type="text/javascript" language="javascript">
   function YourMethodHere(source, eventArgs)
   {
      $get('ctl00_BodyPlaceHolder_btnAutoSubmit').click();
   }
</script>

You'll need to find the proper name of your button accordingly and replace it above.

As an added feature, sometimes I want to be able to type a value in to the AutoComplete and hit the enter key right away if I know what I want. To accomplish this, you'll want to wrap your AutoComplete textbox and button in a panel and set the default button accordingly:

<asp:Panel ID="pnlAutoCompleteStuff" runat="server" DefaultButton="btnAutoSubmit">
   Search: <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
           <cc1:AutoCompleteExtender ID="aceSearch" runat="server"
                                      TargetControlID="txtSearch"                             
                                      ServiceMethod="YourMethodHere"                   
                                      ServicePath="YourServicePath"                                   
                                      MinimumPrefixLength="4"
                                      CompletionInterval="500"
                                      EnableCaching="False"
                                      OnClientItemSelected="AutoCompleteClientMethod"
                                      CompletionSetCount="3">
            </cc1:AutoCompleteExtender>
   <asp:Button ID="btnAutoSubmit" runat="server" Text="Select" />
</asp:Panel


Set the textbox's Autopostback to true.

<asp:TextBox ID="SearchCityBox" CssClass="searchOne" runat="server" Width="500px" Height="18px" AutoPostBack="true" OnTextChanged="SearchCityBox_TextChanged"></asp:TextBox>

Then create an onTextChanged event handler to do the same thing as your button. Or you could just point it to the same event handler your button has


Have a look at onclientitemselected

There is some info here: Implementing Auto-Suggest Using AutoCompleteExtender Control

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜