本文介绍了自动扩展文本框不能通过以下代码工作,请帮我解决AutoExteder文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
This Is My code
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select Country from Country where Country like @Name+'%'", con);
cmd.Parameters.AddWithValue("@Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List<string> CountryNames = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CountryNames.Add(dt.Rows[i][1].ToString());
}
//return CountryNames;
return default(string[]);
}
来源页----
Source Page----
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" ServiceMethod="GetCompletionList"
ServicePath="" TargetControlID="TextBox1" UseContextKey="True">
</cc1:AutoCompleteExtender>
推荐答案
ServicePath=""
应该像
ServicePath="AutoComplete.asmx"
没有WebService:
[]
[ ^ ]
否则,您的代码:
Without WebService :
ASP.Net Ajax AutoCompleteExtender Without Using Web Service[^]
http://www.aspdotnet-suresh.com/2011/05/ajax-autocompleteextender-sample.html[^]
or else, your code :
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static List<string>GetCompletionList(string prefixText, int count, string contextKey)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select Country from Country where Country like @Name+'%'", con);
cmd.Parameters.AddWithValue("@Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List<string> CountryNames = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CountryNames.Add(dt.Rows[i][1].ToString());
}
return CountryNames; // returning List<>, and you were returning String[], which is null..
}
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID="txtcity"
ServicePath="~/WebService.asmx"
ServiceMethod="GetCompletionList"
CompletionInterval="1000"
MinimumPrefixLength="1"
EnableCaching="true"
CompletionSetCount="10"
CompletionListItemCssClass="CssClass"
/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
Connection con = new Connection();
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(string prefixText, int count)
{
DataTable dt = con.GetdataTable("SELECT CityName from citymaster Where CityName like ''" + prefixText + "%''");
List<string> st = new List<string>();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
st.Add(dt.Rows[i]["CityName"].ToString());
}
}
return (st.ToArray());
}
}
这篇关于自动扩展文本框不能通过以下代码工作,请帮我解决AutoExteder文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!