我的WebService.asmx页面代码是: [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { string sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString; public WebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string[] getWordList(string prefixText, int count) { SqlConnection sqlcon = new SqlConnection(sqlconn); string mysql = "select Top "+count+" UserName from [user] where UserName like " + "'" + prefixText + "%'"; SqlDataAdapter da = new SqlDataAdapter(mysql, sqlcon); DataSet ds = new DataSet(); da.Fill(ds, "user"); string[] returnvalue = new string[ds.Tables["user"].Rows.Count]; for (int i = 0; i < ds.Tables["user"].Rows.Count; i++) { returnvalue[i] = ds.Tables["user"].Rows[i][0].ToString(); } return (returnvalue); } }Default.aspx页面代码是: <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" MinimumPrefixLength="1" ServiceMethod="getWordList" ServicePath="WebService.asmx" TargetControlID="TextBox1"> </cc1:AutoCompleteExtender>页面没有实现这种效果 请问上面哪里有误?
在www.g.cn搜索asp.net ajax control toolkit,然后安装在VS中。安装好后里面有一控件AutoComplete。具体用法你可以看看赵劼ASP.NET AJAX方面的视频等信息资料。很简单就能实现的。
还是自己写javascript控制比较好效率是一个方面 要速度快和通常的ajax差不多
1、aspx的ScriptManager没必要引用WebService,粗体部分没必要。 <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="AutoSelect.asmx" /> </Services> </asp:ScriptManager> 2、CompletionSetCount="20"指定的这个20会传递到web service的方法(string prefixText, int count)的count。 3、 [WebMethod] public string[] GetQymc(string prefixText, int count) { System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(); using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = "server=localhost;database=数据库名;uid=sa;pwd="; SqlCommand cmd = new SqlCommand(@"select 字段 from (select 字段, ROW_NUMBER() OVER (ORDER BY 字段 desc) as MyRank from 表名 where 字段 like @prefix) as tempTable where MyRank <= @count", conn);
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService { string sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
public WebService () { //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public string[] getWordList(string prefixText, int count)
{
SqlConnection sqlcon = new SqlConnection(sqlconn);
string mysql = "select Top "+count+" UserName from [user] where UserName like " + "'" + prefixText + "%'";
SqlDataAdapter da = new SqlDataAdapter(mysql, sqlcon);
DataSet ds = new DataSet();
da.Fill(ds, "user");
string[] returnvalue = new string[ds.Tables["user"].Rows.Count];
for (int i = 0; i < ds.Tables["user"].Rows.Count; i++)
{
returnvalue[i] = ds.Tables["user"].Rows[i][0].ToString();
}
return (returnvalue);
}
}Default.aspx页面代码是:
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" MinimumPrefixLength="1" ServiceMethod="getWordList" ServicePath="WebService.asmx" TargetControlID="TextBox1">
</cc1:AutoCompleteExtender>页面没有实现这种效果
请问上面哪里有误?
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="AutoSelect.asmx" />
</Services>
</asp:ScriptManager> 2、CompletionSetCount="20"指定的这个20会传递到web service的方法(string prefixText, int count)的count。 3、
[WebMethod]
public string[] GetQymc(string prefixText, int count) {
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(); using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "server=localhost;database=数据库名;uid=sa;pwd=";
SqlCommand cmd = new SqlCommand(@"select 字段 from
(select 字段, ROW_NUMBER() OVER (ORDER BY 字段 desc) as MyRank from 表名 where 字段 like @prefix) as tempTable
where MyRank <= @count", conn);
cmd.Parameters.AddWithValue("prefix", prefixText + "%");
cmd.Parameters.AddWithValue("count", count);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
while (reader.Read())
{
list.Add(reader.GetString(0) + "," + count);
}
} return list.ToArray();
}
搞定了
原来是我在web配置文件中没有配置好Ajax