【问题标题】:Ajax Control Toolkit Autocomplete extenderAjax Control Toolkit 自动完成扩展器
【发布时间】:2012-10-07 15:50:13
【问题描述】:

我已按照本指南 (http://www.asp.net/ajaxlibrary/act_AutoComplete_simple.ashx) 使用自动完成扩展器,但它可以在我的大型项目中使用,但我无法终生使用看到不同。将扩展器嵌套在表格元素中是否有问题?

无论如何,我有自动完成扩展程序调用教程中的哑巴方法只是为了开始。不使用网络服务,而只是一种方法(如指南中所示)。该页面使用母版页,是否已知会导致问题?这是标题

<%@ 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>

和文本框:

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

以及背后的代码:

[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();
}

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

因此,它们必须是 Masterpage 或 web.config 中的某些设置会改变工具包的行为。有什么想法吗?

编辑 2: 我刚刚尝试将 ToolScriptManager 放在母版页中 - 没有骰子;和... 添加了

EnabledPageMethods="true"

到 ToolScriptManager - 仍然没有骰子。

来自 web.config 的最后一个相关 sn-p:

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

【问题讨论】:

  • GetCompletionList4 是否与您项目中的完全相同? Where 方法内容周围缺少括号。

标签: c# asp.net .net autocomplete ajaxcontroltoolkit


【解决方案1】:

我放弃了 Ajax Control Toolkit。这是一个 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
            });
        });

...以及背后的代码:

    [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[];
   }

并将这些放在 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" />

【讨论】:

    【解决方案2】:

    这是我的解决方案,我正在使用 web 服务来调用自动完成功能。

    假设您已正确安装 AjaxControlToolKit,请按照以下步骤操作

    在母版页中

    1.在 .aspx 页面顶部添加以下行

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

    2。在表单 id="form1" runat="server"

    之后添加以下行
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
       <Services>
          <asp:ServiceReference Path="~/AutoComplete.asmx" />
       </Services>
    </asp:ToolkitScriptManager>
    

    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.创建网络服务

    Solution Explorer -> Right Clic -> Add New Item... -> Web Service(我将它重命名为 AutoComplete.asmx)然后按下按钮 Add

    在 Web 服务 AutoComplete.asmx 中

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

    '&lt;System.Web.Script.Services.ScriptService()&gt; _

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

    6.添加您的 asp:AutoCompleteExtender ServiceMethod 称为 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
    

    注意:照顾好

    <System.Web.Services.WebMethod()>
    

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

    刷新您的网页并进行测试

    希望对你和未来的其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      相关资源
      最近更新 更多