AJAX AutocompleteExtender 不起作用。 网络服务工作
这是 c# .net 2.0。 我正在使用母版页。
- WebService 本身工作得很好。
- 我完全被难住了。 当我在文本框中输入内容时,没有任何反应。
文件:
EditTicket.aspx 自动完成.asmx App_Code/AutoComplete.cs
EditTicket.aspx:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc2" %>
<asp:ScriptManager id="ScriptManager1" runat="server" EnablepageMethods="true">
<Services>
<asp:ServiceReference Path="AutoComplete.asmx" />
</Services>
</asp:ScriptManager>
<cc2:AutoCompleteExtender
runat="server"
ID="AutoCompleteExtender1"
ServicePath="AutoComplete.asmx"
ServiceMethod="AutoComplete2"
MinimumPrefixLength="1"
CompletionSetCount="12"
TargetControlID="TextBox3"
EnableCaching="True" >
</cc2:AutoCompleteExtender>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
AutoComplete.asmx:
<%@ WebService Language="C#" CodeBehind="~/App_Code/AutoComplete.cs" Class="AutoComplete" %>
AutoComplete.cs:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class AutoComplete : System.Web.Services.WebService {
public AutoComplete () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod]
public string[] AutoComplete2(string prefixText,int count)
{
string conString = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
SqlConnection connection = new SqlConnection(conString);
connection.Open();
SqlParameter prm;
string sql = "Select program_name FROM CM_Programs WHERE program_name LIKE @prefixText";
SqlDataAdapter cmd = new SqlDataAdapter(sql, connection);
prm = new SqlParameter("@prefixText", SqlDbType.VarChar, 50);
prm.Value = prefixText+ "%";
cmd.SelectCommand.Parameters.Add(prm);
DataTable dt = new DataTable();
cmd.Fill(dt);
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr["program_name"].ToString(),i);
i++;
}
connection.Close();
return items;
}
}
This is c# .net 2.0. I am using a masterpage.
- The WebService works fine on its own.
- I am completely stumped. When I type in the TextBox, nothing happens.
Files:
EditTicket.aspx
AutoComplete.asmx
App_Code/AutoComplete.cs
EditTicket.aspx:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc2" %>
<asp:ScriptManager id="ScriptManager1" runat="server" EnablepageMethods="true">
<Services>
<asp:ServiceReference Path="AutoComplete.asmx" />
</Services>
</asp:ScriptManager>
<cc2:AutoCompleteExtender
runat="server"
ID="AutoCompleteExtender1"
ServicePath="AutoComplete.asmx"
ServiceMethod="AutoComplete2"
MinimumPrefixLength="1"
CompletionSetCount="12"
TargetControlID="TextBox3"
EnableCaching="True" >
</cc2:AutoCompleteExtender>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
AutoComplete.asmx:
<%@ WebService Language="C#" CodeBehind="~/App_Code/AutoComplete.cs" Class="AutoComplete" %>
AutoComplete.cs:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class AutoComplete : System.Web.Services.WebService {
public AutoComplete () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod]
public string[] AutoComplete2(string prefixText,int count)
{
string conString = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
SqlConnection connection = new SqlConnection(conString);
connection.Open();
SqlParameter prm;
string sql = "Select program_name FROM CM_Programs WHERE program_name LIKE @prefixText";
SqlDataAdapter cmd = new SqlDataAdapter(sql, connection);
prm = new SqlParameter("@prefixText", SqlDbType.VarChar, 50);
prm.Value = prefixText+ "%";
cmd.SelectCommand.Parameters.Add(prm);
DataTable dt = new DataTable();
cmd.Fill(dt);
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr["program_name"].ToString(),i);
i++;
}
connection.Close();
return items;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“什么也没发生”并不是一个简单的描述。 当你说没有任何反应时,你是否检查过
网络服务?
返回结果?
正确吗?
如果“没有”是指上述情况都没有发生,我将开始检查页面上是否没有 javascript 错误,以及您的 AutoComplete 扩展程序是否正确呈现(检查跟踪中的页面控件)。
"Nothing happens" is not an easy description to go on. When you say nothing happens, have you checked that
web service?
returning results?
correctly?
If "nothing" is that none of the above is happening, I would start checking that there are no javascript errors on the page and that your AutoComplete extender is rendering correctly (examine the page controls in a trace).
尝试摆弄 CompletionInterval 属性。 我过去曾使用过此控件,但在将 CompletionInterval 设置为低得多的值之前,没有看到预期的行为。 它默认为 1000(毫秒),我会尝试将其设置为 1,只是为了看看一切是否正常工作(并且 womp 的步骤应该有助于缩小发生通信问题的范围)以及是否正常工作,继续增加该值,直到达到一个有意义的值(1 毫秒向服务器发送大量请求)。 报告哪些有效,哪些无效。
Try fiddling with the CompletionInterval property. I have used this control in the past and wasn't seeing the behavior I expected until I set the CompletionInterval to a much lower value. It defaults to 1000 (ms), I would give it a shot with a value of 1, just to see if everything is working as it should (and womp's steps should help to narrow down where the communication issues are happening) and if it does work, keep increasing the value until you hit a value that makes sense (1 ms sends a lot of requests to the server). Report back on what works and what doesn't.