Asp.net ajax 自动完成无法与 AutoCompleteExtender 一起使用
Asp.net ajax autocomplete not working with AutoCompleteExtender
我想在我的文本框中实现 ajax autocomplete
功能,
我试过如下
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<ajax:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtCountry"
ServicePath="~/App_Code/WebService.cs" MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1"
CompletionInterval="1000" ServiceMethod="GetCompletionList">
</ajax:AutoCompleteExtender>
并在 Webservice.cs
我试过如下
[WebMethod]
public List<string> GetCompletionList(string prefixText)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select Company_name from Outword_CommonMST where Company_name " +
"like '" + prefixText + "%' order by company_name", con);
//cmd.Parameters.AddWithValue("@Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable( );
da.Fill(dt);
List<string> Company_name = new List<string>();
for(int i=0;i<dt.Rows.Count;i++)
{
Company_name.Add(dt.Rows[i][1].ToString());
}
return Company_name;
}
}
但是当我检查它时,它不起作用。
另外,当我检查控制台时,它给出了类似
的信息
ScriptResource.axd?d=smCBrjG8b3peouydxeU-NDM-jIwTmeeFry083DH5OhjfnWTee0cYnYTLGZX9W7p4AmkPzE7GkZ-MoV…:6076 POST http://localhost:55264/PSITERP%20lite/App_Code/WebService.cs/GetCompletionList 403 (Forbidden)
请指出这里有什么问题
像这样:
[WebMethod]
public string[] GetCompletionList(string prefixText)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select Company_name from Outword_CommonMST where Company_name " +
"like '" + prefixText + "%' order by company_name", con);
//cmd.Parameters.AddWithValue("@Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable( );
da.Fill(dt);
List<string> Company_name = new List<string>();
for(int i=0;i<dt.Rows.Count;i++)
{
Company_name.Add(dt.Rows[i][0].ToString());
}
return Company_name.ToArray();
}
}
我想在我的文本框中实现 ajax autocomplete
功能,
我试过如下
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<ajax:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtCountry"
ServicePath="~/App_Code/WebService.cs" MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1"
CompletionInterval="1000" ServiceMethod="GetCompletionList">
</ajax:AutoCompleteExtender>
并在 Webservice.cs
我试过如下
[WebMethod]
public List<string> GetCompletionList(string prefixText)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select Company_name from Outword_CommonMST where Company_name " +
"like '" + prefixText + "%' order by company_name", con);
//cmd.Parameters.AddWithValue("@Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable( );
da.Fill(dt);
List<string> Company_name = new List<string>();
for(int i=0;i<dt.Rows.Count;i++)
{
Company_name.Add(dt.Rows[i][1].ToString());
}
return Company_name;
}
}
但是当我检查它时,它不起作用。
另外,当我检查控制台时,它给出了类似
的信息ScriptResource.axd?d=smCBrjG8b3peouydxeU-NDM-jIwTmeeFry083DH5OhjfnWTee0cYnYTLGZX9W7p4AmkPzE7GkZ-MoV…:6076 POST http://localhost:55264/PSITERP%20lite/App_Code/WebService.cs/GetCompletionList 403 (Forbidden)
请指出这里有什么问题
像这样:
[WebMethod]
public string[] GetCompletionList(string prefixText)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select Company_name from Outword_CommonMST where Company_name " +
"like '" + prefixText + "%' order by company_name", con);
//cmd.Parameters.AddWithValue("@Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable( );
da.Fill(dt);
List<string> Company_name = new List<string>();
for(int i=0;i<dt.Rows.Count;i++)
{
Company_name.Add(dt.Rows[i][0].ToString());
}
return Company_name.ToArray();
}
}