我正在使用 asp.net 4.0 (vs2010)。我正在尝试实现 AutoCompleteExtender ajax 控件。起初我尝试使用页面方法,但由于我在用户控件中有扩展器,因此我无法使用此方法。我花了几天时间研究并确定我需要使用启用 Ajax 的 WCF 服务。所以我做了这个。我脱离了我的应用程序并创建了一个测试应用程序来模仿我正在尝试做的事情并且它可以工作。我将控件放在页面上,将其指向服务,瞧,它会触发事件并且效果很好。因此,我将带有必要更改的代码移植到我的主应用程序中,但它不起作用。不起作用我的意思是它不会触发服务中的事件。我怀疑问题出在 ServicePath 上,但我尝试了很多很多不同的路径,但似乎没有任何效果。
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div style="float: left">
<asp:Label runat="server">Site Id:</asp:Label><br />
<asp:TextBox ID="txtSiteId" runat="server" AutoPostBack="True" OnTextChanged="TextChangedEvent"></asp:TextBox>
</div>
<div style="float: left; width: 200px; padding-left: 20px; margin-right: 5px">
<asp:Label runat="server">Entered Site Id's:</asp:Label><br />
<asp:Repeater ID="rptSiteIds" runat="server" OnItemCommand="rptSiteIds_ItemCommand">
<ItemTemplate>
<asp:LinkButton id="lnkRemove" Width="50" runat="server" Text="remove" CommandName="remove"></asp:LinkButton>
<asp:Label ID="lblItem" runat="server" Text='<%# Eval("Item") %>'></asp:Label>
</ItemTemplate>
<SeparatorTemplate>
<br>
</SeparatorTemplate>
</asp:Repeater>
</div>
<Controls:ConfirmationModal runat="server" ID="MaxSiteIdConfirmation" ModalType="Alert" OnOkClicked="ConfirmationOk_Clicked"
Title="Site Id Validation" Message="Too many site id's"
Width="300" />
<ajaxToolkit:AutoCompleteExtender
runat="server"
ID="AutoCompleteExtender"
TargetControlID="txtSiteId"
CompletionSetCount="10"
UseContextKey="True"
ServicePath="~\DataServices\WebDataServices.svc"
ServiceMethod="GetCompletionList" />
</ContentTemplate>
这是来自服务的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
namespace Web.Internal.DataServices
{
[ServiceContract(Namespace = "WebDataServices")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WebDataServices
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
// Add more operations here and mark them with [OperationContract]
[OperationContract]
public string[] GetCompletionList(string prefixText, int count)
{
//return names;
String[] autoCompleteWordList = (from s in CreateList()
where s.Contains(prefixText)
select s).Take(count).ToArray();
return autoCompleteWordList;
}
private string[] CreateList()
{
using (UnitOfWork uow = new UnitOfWork("DataService"))
{
return new ServiceDetailsBusiness(uow).GetDistinctIds().ToArray();
}
}
}
}
任何帮助,将不胜感激。