AJAX 控制工具包自动完成扩展器不起作用
我无法让 AJAX CT 自动完成工作。问题是当我开始在文本框中写入时什么也没有发生。我遇到的第一个问题是当我尝试添加自动完成页面方法时出现错误:“无法创建页面方法“GetCompletionList”...”。然后我尝试手动创建它,但仍然没有任何反应。
这是 AdministracijaOsoba.aspx 代码:
<asp:TextBox ID="txtOsoba" runat="server"></asp:TextBox><asp:AutoCompleteExtender
ID="AutoCompleteExtender1" runat="server" ScriptPath=""
ServiceMethod="GetCompletionList" ServicePath="AdministracijaOsoba.aspx.cs"
TargetControlID="txtOsoba" UseContextKey="True">
</asp:AutoCompleteExtender>
这是 AdministracijaOsoba.aspx.cs 代码:
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
PravosudnaAkademijaEntities db = new PravosudnaAkademijaEntities();
var osoba = from o in db.osobas
orderby o.osoba_prezime
select new { o.osoba_id, person = o.osoba_prezime + " " + o.osoba_ime };
string[] main = new string[0];
foreach (var o in osoba)
{
if (o.person.StartsWith(prefixText))
{
Array.Resize(ref main, main.Length + 1);
main[main.Length - 1] = o.person.ToString();
if (main.Length == 15)
{
break;
}
}
}
Array.Sort(main);
return main;
}
请注意,我正在使用 LINQ to Entities。对此的任何帮助将不胜感激。
问候!
I can't get AJAX CT Autocomplete to work. The problem is when I start writing in textbox nothing happens. The frist problem I had experienced was when I tried to Add AutoComplete page method I got an error: "Cannot create page method "GetCompletionList"...". Then I tried creating it manually, but still nothing happens.
Here is the AdministracijaOsoba.aspx code:
<asp:TextBox ID="txtOsoba" runat="server"></asp:TextBox><asp:AutoCompleteExtender
ID="AutoCompleteExtender1" runat="server" ScriptPath=""
ServiceMethod="GetCompletionList" ServicePath="AdministracijaOsoba.aspx.cs"
TargetControlID="txtOsoba" UseContextKey="True">
</asp:AutoCompleteExtender>
Here is the AdministracijaOsoba.aspx.cs code:
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
PravosudnaAkademijaEntities db = new PravosudnaAkademijaEntities();
var osoba = from o in db.osobas
orderby o.osoba_prezime
select new { o.osoba_id, person = o.osoba_prezime + " " + o.osoba_ime };
string[] main = new string[0];
foreach (var o in osoba)
{
if (o.person.StartsWith(prefixText))
{
Array.Resize(ref main, main.Length + 1);
main[main.Length - 1] = o.person.ToString();
if (main.Length == 15)
{
break;
}
}
}
Array.Sort(main);
return main;
}
Take a note that I'm using LINQ to Entities. Any help on this would be appreciated.
Regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您的后台代码应如下所示
另外,如果您使用 pagescript 方法,则无需为您的 ajax 扩展程序提供 servicepath 属性。
Your code behind should read like this
Also, there is no need of providing the servicepath attribute for your ajax extender if you are using a pagescriptmethod.
将您的声明更改为:
将其添加到您的 AdministracijaOsoba.aspx.cs 代码中:
Change your declaration to this:
Add this to your AdministracijaOsoba.aspx.cs code:
这是我的 aspx 页面中的内容:
在我的代码隐藏页面中,我有:
Here's what I have in my aspx page:
In my code behind page I have:
你的代码几乎是正确的。唯一的问题是服务路径不应以 .aspx.cs 结尾,而只能以 .aspx 结尾。如果扩展程序与方法位于同一页面上,则省略 servicepath
Your code is almost right. The only problem is that the service path shouldnt end with .aspx.cs but only .aspx. If the extender is on the same page as the method then leave out servicepath
我也遇到了同样的问题。我知道这有点晚了,但迟到总比不到好...
这是最终对我有用的设置(包含您的 ID 和名称):
代码隐藏 ( aspx.cs ) :
代码 ( .aspx ):
如您所见,您不需要设置 ScriptPath 和 ServicePath 属性,因为这些属性代表...
自动完成参考页中对此进行了解释。您已经在代码隐藏中定义了 GetCompletionList() 方法,我目前假设该方法符合“页面方法”的资格。因此,如果我们将该方法放在不同的位置(例如 services.cs 或类似的位置),您似乎只会使用 Path 属性。
I too have been having the same problem. I know this is a bit late, but better late than never...
Here is the setup that finally worked for me (with your IDs and names in place):
Code-Behind ( aspx.cs ):
Code ( .aspx ):
As you can see, you don't need to set the ScriptPath and ServicePath properties because these properties represent...
which is explained in the AutoComplete Reference Page. You've got your GetCompletionList() method defined in your Code-Behind, which I am currently assuming qualifies as "a page method". So, it seems you would only use the Path properties if we had the method in a different location such as a services.cs or something of that sort.
删除方法声明中的
static
关键字。remove the
static
keyword in your method declaration.也许您缺少指定 AutoCompleteExtender 的 MinimumPrefixLength 参数。
Perhaps you are missing specifying MinimumPrefixLength parameter of AutoCompleteExtender.
将 TextBox 的 auto-postback 属性设置为 TRUE
Set auto-postback property of TextBox to TRUE
Ajax 自动完成使用服务调用,因此您可以在 aspx.cs 文件中使用以下代码,注意
System.Web.Services.WebMethodAttribute()
属性,这将使该方法可用于服务调用。或者,您可以使用任何 ASMX 服务 或 WCF 服务,用于广泛且可靠的服务使用。
Ajax autocomplete uses a service call, so you can use the below code in your aspx.cs file, Note the
System.Web.Services.WebMethodAttribute()
attribute, this will make the method accessible for Service call.Alternatively you can use any ASMX service or WCF service for extensive and reliable service use.