Ajax控件工具包自动完成扩展工具包、控件、自动完成、Ajax

2023-09-06 16:57:36 作者:眼角被遗忘の淚°

我一直遵循这个指南(http://www.asp.net/ajaxlibrary/act_AutoComplete_simple.ashx)使用自动完成扩展和它的作品却implimenting到我的大项目时,我不能为我的生活看到的差别。这是个问题,有扩展嵌套withing表元素?

反正,我有自动完成扩展要求从教程刚上手dumbby方法。不使用web服务,而只是一个方法(如在指南)。该页面使用一个母版页,是已知的导致问题吗?继承人的标题

 <%@页标题=报告LANGUAGE =C#的MasterPageFile =〜/医生/ MasterPage.masterAutoEventWireup =真正的codeFILE =generateReport .aspx.cs继承=Doctors_generateReport
maintainScrollPositionOnPostBack =真正的%>
<风格> ...< /风格>
< ASP:内容ID =内容1ContentPlaceHolderID =HeadContent=服务器>
< / ASP:内容>
< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器>
< ASP:toolkitscriptmanager ID =ToolkitScriptManager1=服务器>
< / ASP:toolkitscriptmanager>
    &其中,P类=headingStyle><强>< EM>临床报告< / EM>< / STRONG>< / P>
<表>
 

和文本框:

 < TD类= logicalDivide>目前服用的药物:其中; / TD>
< TD类= logicalDivide>
    < ASP:文本框ID =tbCMed=服务器的CssClass =文本框WIDTH =178pxMAXLENGTH =30字体,名称=宋体的onfocus ={THIS.VALUE ='';} >< / ASP:文本框>
    < ASP:autocompleteextender
        ID =AutoCompleteExtender1
        =服务器
        的TargetControlID =tbCMed
        ServiceMethod =GetCompletionList4UseContextKey =真>
    < / ASP:autocompleteextender>
< / TD>
 

和后面的code:

  [WebMethod的]
[ScriptMethod]
公共静态字符串[] GetCompletionList4(字符串prefixText,诠释计数,串contextKey)
{
   //创建电影阵列
   字符串[]电影= {星球大战,星际迷航,超人,记忆碎片,怪物史莱克,怪物史莱克II};

   //返回匹配的电影
   返回movies.Where(M => m.StartsWith(prefixText,StringComparison.CurrentCultureIgnoreCase)
                。取(计数)
                .ToArray();
}
 
Eclipse里代码自动完成 auto completion的快捷键设置

编辑1: 这个问题是相似的(http://stackoverflow.com/questions/791361/trying-to-get-a-simple-example-of-asp-net-ajax-dropdownlist-autocomplete-extende?rq=1)但像演示中,它适用于自己的,但不是在我的应用程序。

因此​​,他们必须在母版或web.config中的某些设置被改变的工具包行为。任何想法?

编辑2: 我只是试图把ToolScriptManager在母版页 - 没有骰子;和... 添加

  EnabledPageMethods =真
 

到ToolScriptManager - 仍然没有骰子

在web.config中最后一个有关代码段:

 <网页>
  <控制>
    <添加标签preFIX =ASP集结号=AjaxControlToolkit中命名空间=AjaxControlToolkit中/>
  < /控制>
< /页>
<身份冒充=真/>
 

解决方案

下面我的解决办法,我使用Web服务调用函数的自动完成功能。

假设你已经正确AjaxControlToolkit中安装了遵循这个步骤

在母版页

1。添加下面一行在你的.aspx页面顶部

 <%@注册议会=AjaxControlToolkit中命名空间=AjaxControlToolkit中标签preFIX =ASP%>
 

2。添加以下行后的表格ID =Form1的=服务器

 < ASP:ToolkitScriptManager ID =ToolkitScriptManager1=服务器>
   <服务>
      < ASP:服务引用路径=〜/ AutoComplete.asmx/>
   < /服务>
< / ASP:ToolkitScriptManager>
 

3。添加您的文本框和AutoCompleteExtender

 < ASP:文本框ID =tbSearch=服务器>< / ASP:文本框>

< ASP:AutoCompleteExtender
                    的TargetControlID =tbSearch
                    ServicePath =AutoComplete.asmx
                    ServiceMethod =GetCompletionList
                    最低prefixLength =3
                    CompletionInterval =100
                    CompletionSetCount =5
                    EnableCaching =假
                    CompletionListCssClass =CompletionList
                    CompletionListItemCssClass =CompletionListItem
                    CompletionListHighlightedItemCssClass =CompletionListHighlightedItem
                    UseContextKey =真
                    ID =AutoCompleteExtender1
                    =服务器>< / ASP:AutoCompleteExtender>
 

4。创建一个Web服务

解决方案资源管理器 - >右键社区法网 - >添加新项... - > Web服务(我remane它AutoComplete.asmx),然后preSS按钮添加

在Web服务AutoComplete.asmx

5。打开AutoComplete.vb文件,并取消注释以下行

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

在VB中这行是默认的注释,而且它需要允许Web服务从脚本调用,使用ASP.NET AJAX

6。加入你的ASP:AutoCompleteExtender ServiceMethod称为公共职能GetCompletionList

 < System.Web.Services.WebMethod()>
    &其中; System.Web.Script.Services.ScriptMethodAttribute()>
    公共职能GetCompletionList(BYVAL prefixText作为字符串,BYVAL数为整数,BYVAL contextKey作为字符串)作为字符串()
        创建电影阵列
        昏暗的影片()作为字符串= {星球大战,星球大战1,星球大战2,星际迷航3,星球大战,星球大战,超人,超女, 记忆碎片,怪物史莱克,怪物史莱克II}

        '返回匹配的电影
        返回 (
            从M在电影
            凡m.StartsWith(prefixText,StringComparison.CurrentCultureIgnoreCase)
            选择M)。取(计数).ToArray()

    端功能
 

注:照顾

 < System.Web.Services.WebMethod()>
 

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

刷新你的网页,并测试它

我希望帮助你和未来的人。

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. 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. Not using a webservice but just a method (like in the guide). 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.

Therefore their must be some settings in the Masterpage or web.config that are altering the toolkits behavior. Any ideas ?

Edit 2: I've just tried putting the ToolScriptManager in the master page - no dice ; and... added

EnabledPageMethods="true"

to the ToolScriptManager - still no dice.

One last relevant snippet from the web.config:

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

解决方案

Here my solution, I'm using webservices to call a function for autocomplete.

Assuming that you have AjaxControlToolKit correctly installed follow this steps

In master page

1. Add the following line at top of your .aspx page

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

2. Add the following line after form 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

<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

Solution Explorer -> Right Clic -> Add New Item... -> Web Service (I remane it to AutoComplete.asmx) and then press button Add

In Web Services AutoComplete.asmx

5. Open AutoComplete.vb file and uncomment the following line

'<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

6. Add your asp:AutoCompleteExtender ServiceMethod called Public Function GetCompletionList

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