我正在尝试在我的项目中使用自动完成扩展程序,但它无法正常工作,我使用的是Vs2008 .net framework 3.5 并且它给了我网格视图分页的错误,该源不可用 这是我的.aspx代码
<asp:TextBox ID="txtsearch" runat="server" Width="158px" CssClass="text"
Height="22px" ontextchanged="txtsearch_TextChanged"/>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
ServiceMethod="GetFilterCategoryName" CompletionSetCount="1" CompletionInterval="10"
EnableCaching="true" MinimumPrefixLength="1" TargetControlID="txtsearch"
UseContextKey="true">
</cc1:AutoCompleteExtender>
这是我的Axpx.cs代码
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public static List<string> GetFilterCategoryName(string prefixText)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select Question from quick_search where Question like @Name+'%'", conn);
cmd.Parameters.AddWithValue("@Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List<string> answer = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
answer.Add(dt.Rows[i][1].ToString());
}
return answer;
}
答案 0 :(得分:0)
将脚本管理器添加到aspx文件中。
试试这个
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true"></asp:ScriptManager>
<asp:TextBox ID="txtsearch" runat="server" Width="158px" CssClass="text" Height="22px" ontextchanged="txtsearch_TextChanged"/>
<cc1:AutoCompleteExtender ServiceMethod="GetFilterCategoryName"
MinimumPrefixLength="1"
CompletionInterval="0" EnableCaching="false" CompletionSetCount="10"
TargetControlID="txtsearch"
ID="autoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>
COED背后:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public static List<string> GetFilterCategoryName(string prefixText)
{
SqlConnection conn = new sqlConnection(ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select Question from quick_search where Question like @Name+'%'", conn);
cmd.Parameters.AddWithValue("@Name", prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List<string> answer = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
answer.Add(dt.Rows[i][1].ToString());
}
return answer;
}