已经写好WebService和页面内容。可能有错。突然忘了TextBox_TextChanged中应该怎么写。要实现在文本框中输入单个内容进行模糊查询。//页面内容
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:Label ID="Label1" runat="server" Text="产品名"></asp:Label>
<asp:TextBox ID="txtSearch" runat="server" ontextchanged="txtSearch_TextChanged" ></asp:TextBox>
<asp:AutoCompleteExtender
ID="AutoCompleteExtenderSearch"
MinimumPrefixLength="1"
CompletionInterval="10"
runat="server"
EnableCaching="true"
ServicePath="WebService.asmx"
ServiceMethod="GetCompletionList"
TargetControlID="txtSearch">
</asp:AutoCompleteExtender>
</div>
</form>
</body>
</html>//WebServiceusing System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
namespace webApp
{
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
} [WebMethod]
public string[] GetCompletionList(string prefixText,int count)
{
List<String> list = new List<string>(count); SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=miyouba"); string sql = "select top" + count + " * from miyouba where Name like '%" + prefixText + "%'"; conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader reader = cmd.ExecuteReader(); string name; while (reader.Read())
{
name = reader["Name"].ToString().Trim();
list.Add("'" + name + "'");
} conn.Close();
return list.ToArray(); }
}
}望各位大哥大姐指教下。小弟不才。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:Label ID="Label1" runat="server" Text="产品名"></asp:Label>
<asp:TextBox ID="txtSearch" runat="server" ontextchanged="txtSearch_TextChanged" ></asp:TextBox>
<asp:AutoCompleteExtender
ID="AutoCompleteExtenderSearch"
MinimumPrefixLength="1"
CompletionInterval="10"
runat="server"
EnableCaching="true"
ServicePath="WebService.asmx"
ServiceMethod="GetCompletionList"
TargetControlID="txtSearch">
</asp:AutoCompleteExtender>
</div>
</form>
</body>
</html>//WebServiceusing System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
namespace webApp
{
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
} [WebMethod]
public string[] GetCompletionList(string prefixText,int count)
{
List<String> list = new List<string>(count); SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=miyouba"); string sql = "select top" + count + " * from miyouba where Name like '%" + prefixText + "%'"; conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader reader = cmd.ExecuteReader(); string name; while (reader.Read())
{
name = reader["Name"].ToString().Trim();
list.Add("'" + name + "'");
} conn.Close();
return list.ToArray(); }
}
}望各位大哥大姐指教下。小弟不才。
protected void TextBox_TextChanged(object sender, EventArgs e)
{
Response.Write(TextBox1.Text);
}