关于asp.net Ajax AutoCompleteExtender自动完成匹配功能

wangjinchang 2009-06-04 01:24:14
我要完成搜索新闻标题。用了如题所写的控件,但就是提示不出来,直接上代码,请高手帮我看看。谢谢。(我想要Google的效果)
前台页面Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1"

ServicePath="KeyFind.asmx" CompletionSetCount="10" MinimumPrefixLength="1" ServiceMethod="GetCompleteDepart">

</cc1:AutoCompleteExtender>


</div>
</form>
</body>
</html>

操作数据库KeyFind.asmx:

using System;

using System.Web;

using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;

//引入空间

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

/// <summary>

/// KeyFind 的摘要说明

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

//添加服务脚本(必须添,否则程序不能正常运行)

[System.Web.Script.Services.ScriptService]

public class KeyFind : System.Web.Services.WebService
{



public KeyFind()
{

//如果使用设计的组件,请取消注释以下行

//InitializeComponent();

}

//定义数组保存获取的内容

private string[] autoCompleteWordList = null;

//两个参数“prefixText”表示用户输入的前缀,count表示返回的个数

[WebMethod]

public String[] GetCompleteDepart(string prefixText, int count)
{

///检测参数是否为空

if (string.IsNullOrEmpty(prefixText) == true || count <= 0) return null;

// 如果数组为空

if (autoCompleteWordList == null)
{

//读取数据库的内容

SqlConnection conn = new SqlConnection("server=192.168.1.185;database=ttxs;uid=sa;pwd=sa");

conn.Open();

SqlDataAdapter da = new SqlDataAdapter("select Title from EduWare where Title like'" + prefixText + "%' order by Title", conn);

DataSet ds = new DataSet();

da.Fill(ds);

//读取内容文件的数据到临时数组

string[] temp = new string[ds.Tables[0].Rows.Count];

int i = 0;

foreach (DataRow dr in ds.Tables[0].Rows)
{

temp[i] = dr["Title"].ToString();

i++;

}

Array.Sort(temp, new CaseInsensitiveComparer());

//将临时数组的内容赋给返回数组

autoCompleteWordList = temp;

if (conn.State == ConnectionState.Open)

conn.Close();

}

//定位二叉树搜索的起点

int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());

if (index < 0)
{ //修正起点

index = ~index;

}

//搜索符合条件的数据

int matchCount = 0;

for (matchCount = 0; matchCount < count && matchCount + index < autoCompleteWordList.Length; matchCount++)
{ ///查看开头字符串相同的项

if (autoCompleteWordList[index + matchCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) == false)
{

break;

}

}

//处理搜索结果

string[] matchResultList = new string[matchCount];

if (matchCount > 0)
{ //复制搜索结果

Array.Copy(autoCompleteWordList, index, matchResultList, 0, matchCount);

}

return matchResultList;

}

}


...全文
393 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
taopaoH 2011-04-19
  • 打赏
  • 举报
回复
.net3.5 和vs2008中使用Ajax控件,都要将<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
改为<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnableScriptGlobalization="True" EnableScriptLocalization="True">
</asp:ToolkitScriptManager>,否则无法显示
wangqingsong82 2011-04-10
  • 打赏
  • 举报
回复
我刚才也找了这段代码,是不是本身就有Bug,怎么也调试不出来呢。。晕。
xu_119 2010-11-03
  • 打赏
  • 举报
回复
好像要把web.config配置一下
xcltl618 2010-08-13
  • 打赏
  • 举报
回复
我的还是不能自动匹配,无语啊
never1031 2009-12-09
  • 打赏
  • 举报
回复
哭。。今天我也找了这段代码。。怎么不行的?
xu_119 2009-11-11
  • 打赏
  • 举报
回复
关注
tzqmm88 2009-08-18
  • 打赏
  • 举报
回复
我也遇到相似问题了,关注中
fengying0529 2009-07-20
  • 打赏
  • 举报
回复
为什么不能显示出来呢
fzhjkk 2009-07-17
  • 打赏
  • 举报
回复
声明一下 我用的是三层架构写的

如果你没解决的话

你想要源代码的话可以 联系我 QQ:78861401
fzhjkk 2009-07-17
  • 打赏
  • 举报
回复

SearchWord.aspx 页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SearchWord.aspx.cs" Inherits="MyCodeRoom.Web.MyAjax.SearchWord" %>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
请输入关键字<asp:TextBox ID="txtKeyWord" runat="server"></asp:TextBox>

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:AutoCompleteExtender
ID="AutoCompleteExtender1" CompletionSetCount="10"
EnableCaching="true" MinimumPrefixLength="1"
ServiceMethod="GetKeyWords"
ServicePath="../MyWebService/GetKeyWord.asmx"
TargetControlID="txtKeyWord"
runat="server">
</cc1:AutoCompleteExtender>
</div>
</form>
</body>
</html>


GetKeyWord.asmx web service的代码

namespace MyCodeRoom.Web.MyWebService
{
/// <summary>
/// GetKeyWord 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class GetKeyWord : System.Web.Services.WebService
{

[WebMethod]
public string[] GetKeyWords(string prefixText, int count)
{
return SearchKeyWordManager.GetKeyWords(prefixText, count);
}
}
}

wangjinchang 2009-06-04
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 spark_wu 的回复:]
不好意思,好像不是这个错误。呵呵
[/Quote]
谢谢

恩,不时这错误!页不报错。就是不出来提示!郁闷啊! 这个代码是我上网找的
spark_wu 2009-06-04
  • 打赏
  • 举报
回复
不好意思,好像不是这个错误。呵呵
spark_wu 2009-06-04
  • 打赏
  • 举报
回复
SqlDataAdapter da = new SqlDataAdapter("select Title from EduWare where Title like'"+prefixText+"%'"+" order by Title", conn);
spark_wu 2009-06-04
  • 打赏
  • 举报
回复
SqlDataAdapter da = new SqlDataAdapter("select Title from EduWare where Title like'" + prefixText + "%' order by Title", conn);
改成

SqlDataAdapter da = new SqlDataAdapter("select Title from EduWare where Title like'"+prefixText+"%'"+“ order by Title", conn);
shagoo 2009-06-04
  • 打赏
  • 举报
回复
用 firebug 看看网络请求 是不是请求的地址有问题 或者返回本身就是空~
ai_li7758521 2009-06-04
  • 打赏
  • 举报
回复
关注
mark620 2009-06-04
  • 打赏
  • 举报
回复
调试一下看看。
wangjinchang 2009-06-04
  • 打赏
  • 举报
回复
不报错,就是不出来提示。智能匹配出不来
wuyq11 2009-06-04
  • 打赏
  • 举报
回复
shagoo 2009-06-04
  • 打赏
  • 举报
回复
顶~ 有什么报错?或是现象
加载更多回复(1)

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧