[英]Ajax Control Toolkit Autocomplete extender
我遵循了本指南(http://www.asp.net/ajaxlibrary/act_AutoComplete_simple.ashx)來使用自動完成擴展器,但是它可以正常工作,但是當我將其嵌入到較大的項目中時,我無法終其一生。 將擴展器嵌套在表格元素中是否有問題?
無論如何,我有一個自動完成擴展程序,從本教程開始只是調用dumbby方法。 不使用Web服務,而只是使用一種方法(如本指南中所述)。 該頁面使用母版頁,是否已知會導致問題? 這是標題
<%@ 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的最后一個相關代碼段:
<pages>
<controls>
<add tagPrefix="asp" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit"/>
</controls>
</pages>
<identity impersonate="true"/>
我放棄了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" />
在這里,我的解決方案是使用webservices調用自動完成功能。
假設您已正確安裝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.創建一個Web服務
解決方案資源管理器-> Right Clic->添加新項...-> Web服務(我將其重新命名為AutoComplete.asmx),然后按添加按鈕
在Web服務中,AutoComplete.asmx
5.打開AutoComplete.vb文件,並取消注釋以下行
'<System.Web.Script.Services.ScriptService()> _
在VB中,默認情況下,此行為注釋,使用ASP.NET AJAX允許從腳本調用Web服務是必需的
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
注意:注意
<System.Web.Services.WebMethod()>
和
<System.Web.Script.Services.ScriptMethodAttribute()>
刷新網頁並進行測試
希望對您和未來的其他人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.