简体   繁体   English

Ajax Control Toolkit自动完成扩展器

[英]Ajax Control Toolkit Autocomplete extender

I've followed this guide (http://www.asp.net/ajaxlibrary/act_AutoComplete_simple.ashx) to use the autocomplete extender and it works however when implimenting into my larger project i can't for the life of me see the difference. 我遵循了本指南(http://www.asp.net/ajaxlibrary/act_AutoComplete_simple.ashx)来使用自动完成扩展器,但是它可以正常工作,但是当我将其嵌入到较大的项目中时,我无法终其一生。 Is it a problem to have the extender nested withing table elements? 将扩展器嵌套在表格元素中是否有问题?

anyway, i have the auto complete extender calling a dumbby method from the tutorial just to get started. 无论如何,我有一个自动完成扩展程序,从本教程开始只是调用dumbby方法。 Not using a webservice but just a method (like in the guide). 不使用Web服务,而只是使用一种方法(如本指南中所述)。 The page uses a master page, is that known to cause problems? 该页面使用母版页,是否已知会导致问题? heres the header 这是标题

<%@ Page Title="Report" Language="C#" MasterPageFile="~/Doctors/MasterPage.master" AutoEventWireup="true" CodeFile="generateReport.aspx.cs" Inherits="Doctors_generateReport"
maintainScrollPositionOnPostBack="true" %>
<style>...</style>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:toolkitscriptmanager ID="ToolkitScriptManager1" runat="server" >
</asp:toolkitscriptmanager>
    <p class="headingStyle"><strong><em>Clinical Report</em></strong></p>
<table>

and the textbox: 和文本框:

<td class=logicalDivide>Current Medication:</td>
<td class=logicalDivide>
    <asp:TextBox ID="tbCMed" runat="server" CssClass="textbox" Width="178px" MaxLength="30" Font-Names="Calibri" onfocus="{ this.value = ''; }"></asp:TextBox>
    <asp:autocompleteextender
        ID="AutoCompleteExtender1" 
        runat="server"
        TargetControlID="tbCMed"
        ServiceMethod="GetCompletionList4" UseContextKey="True">
    </asp:autocompleteextender>
</td>

and the code behind: 以及后面的代码:

[WebMethod]
[ScriptMethod]
public static string[] GetCompletionList4(string prefixText, int count, string contextKey)
{
   // Create array of movies  
   string[] movies = { "Star Wars", "Star Trek", "Superman", "Memento", "Shrek", "Shrek II" };

   // Return matching movies  
   return movies.Where(m => m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase)
                .Take(count)
                .ToArray();
}

Edit 1: This question is similar (http://stackoverflow.com/questions/791361/trying-to-get-a-simple-example-of-asp-net-ajax-dropdownlist-autocomplete-extende?rq=1) but like the demo, it works on its own but not in my application. 编辑1:这个问题是类似的(http://stackoverflow.com/questions/791361/trying-to-get-a-simple-example-of-asp-net-ajax-dropdownlist-autocomplete-extende?rq=1)但是像演示一样,它可以独立运行,但不能在我的应用程序中运行。

Therefore their must be some settings in the Masterpage or web.config that are altering the toolkits behavior. 因此,它们必须是Masterpage或web.config中的某些更改工具箱行为的设置。 Any ideas ? 有任何想法吗 ?

Edit 2: I've just tried putting the ToolScriptManager in the master page - no dice ; 编辑2:我只是尝试将ToolScriptManager放在母版页中-没有骰子; and... added 和...添加

EnabledPageMethods="true"

to the ToolScriptManager - still no dice. 到ToolScriptManager-仍然没有骰子。

One last relevant snippet from the web.config: 来自web.config的最后一个相关代码段:

<pages>
  <controls>
    <add tagPrefix="asp" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit"/>
  </controls>
</pages>
<identity impersonate="true"/>

I gave up on the Ajax Control Toolkit. 我放弃了Ajax Control Toolkit。 Heres a jQuery solution (noticable faster than the Control Toolkit ...before it stoped working!!): 继承人的jQuery解决方案(比Control Toolkit显着快...在停止工作之前!!):

<div class="ui-widget">

    <asp:TextBox ID="tbScripts" ClientIDMode="static" runat="server" CssClass="textbox" 
            Width="340px" MaxLength="20" Font-Names="Calibri"  onfocus="{ this.value = ''; }"
                ToolTip="add a medication/script to the managment plan"></asp:TextBox>

        <script type="text/javascript" >
        PageMethods.GetMeds(function (results) {
            $('#tbScripts').autocomplete({
                source: results,
                minLength: 3
            });
        });

... and the code behind: ...以及背后的代码:

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
   public static string[] GetMeds()//prefixText)//string prefixText, int count, string contextKey)
   {
      /* ------ database query goes here ----------- */
      return results[];
   }

and put these inside the scriptManager: 并将它们放在scriptManager中:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<link rel="Stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />

Here my solution, I'm using webservices to call a function for autocomplete. 在这里,我的解决方案是使用webservices调用自动完成功能。

Assuming that you have AjaxControlToolKit correctly installed follow this steps 假设您已正确安装AjaxControlToolKit,请按照以下步骤操作

In master page 在母版页中

1. Add the following line at top of your .aspx page 1.在.aspx页面顶部添加以下行

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

2. Add the following line after form id="form1" runat="server" 2.在表单id =“ form1” runat =“ server”之后添加以下行

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
   <Services>
      <asp:ServiceReference Path="~/AutoComplete.asmx" />
   </Services>
</asp:ToolkitScriptManager>

3. Add your textbox and the AutoCompleteExtender 3.添加您的文本框和AutoCompleteExtender

<asp:TextBox ID="tbSearch" runat="server"></asp:TextBox>

<asp:AutoCompleteExtender 
                    TargetControlID="tbSearch"
                    ServicePath="AutoComplete.asmx"
                    ServiceMethod="GetCompletionList"
                    MinimumPrefixLength="3"
                    CompletionInterval="100"
                    CompletionSetCount="5"
                    EnableCaching="false"
                    CompletionListCssClass="CompletionList"
                    CompletionListItemCssClass="CompletionListItem"
                    CompletionListHighlightedItemCssClass="CompletionListHighlightedItem"
                    UseContextKey="True"
                    ID="AutoCompleteExtender1" 
                    runat="server"></asp:AutoCompleteExtender>

4. Create a webservice 4.创建一个Web服务

Solution Explorer -> Right Clic -> Add New Item... -> Web Service (I remane it to AutoComplete.asmx) and then press button Add 解决方案资源管理器-> Right Clic->添加新项...-> Web服务(我将其重新命名为AutoComplete.asmx),然后按添加按钮

In Web Services AutoComplete.asmx 在Web服务中,AutoComplete.asmx

5. Open AutoComplete.vb file and uncomment the following line 5.打开AutoComplete.vb文件,并取消注释以下行

'<System.Web.Script.Services.ScriptService()> _

In VB this line is comment by default, and it's needed for allow Web Service to be called from script, using ASP.NET AJAX 在VB中,默认情况下,此行为注释,使用ASP.NET AJAX允许从脚本调用Web服务是必需的

6. Add your asp:AutoCompleteExtender ServiceMethod called Public Function GetCompletionList 6.添加名为Public Function GetCompletionList的asp:AutoCompleteExtender ServiceMethod

<System.Web.Services.WebMethod()>
    <System.Web.Script.Services.ScriptMethodAttribute()>
    Public Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()
        ' Create array of movies
        Dim movies() As String = {"Star Wars", "Star Wars 1", "Star Wars 2", "Star Trek 3", "Star Wars", "Star Wars", "Superman", "Super woman", "Memento", "Shrek", "Shrek II"}

        ' Return matching movies
        Return (
            From m In movies
            Where m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase)
            Select m).Take(count).ToArray()

    End Function

NOTE: take care of 注意:注意

<System.Web.Services.WebMethod()>

and

<System.Web.Script.Services.ScriptMethodAttribute()>

Refresh your web page and test it 刷新网页并进行测试

I hope help you and future others. 希望对您和未来的其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM