C# 在c中的textbox控件上使用jquery自动完成
当我运行这段代码时,我得到一个警告,说有错误 我的代码:C# 在c中的textbox控件上使用jquery自动完成,c#,asp.net,jquery-ui,C#,Asp.net,Jquery Ui,当我运行这段代码时,我得到一个警告,说有错误 我的代码: <script type="text/javascript"> debugger; $(document).ready(function () { SearchText(); }); function SearchText() { $(".auto").autocomplete({ source: function (request, re
<script type="text/javascript">
debugger;
$(document).ready(function () {
SearchText();
});
function SearchText() {
$(".auto").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetAutoCompleteData",
data: "{'fname':'" + document.getElementById('txtCategory').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
</script>
[WebMethod]
public static List<string> GetAutoCompleteData(string CategoryName)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select fname from tblreg where fname LIKE '%'+@CategoryText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@CategoryText", CategoryName);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["fname"].ToString());
}
return result;
}
}
}
我想调试函数GetAutoCompleteData,但根本没有触发断点。
这个代码有什么问题?请导游。
我附上了上面的屏幕截图。您需要在方法上添加[WebMethod]属性。所以你的方法是这样的
[WebMethod]
public static List<string> GetAutoCompleteData(string CategoryName)
{
....
如何使用web方法
编辑1
在编写url:GetAutoCompleteData的jQuery中,需要指定类或页面名称,然后指定方法名称
您不能直接调用方法。您需要使用class.aspx页面,然后是方法。您需要更改数据属性的值,即$.ajax调用,以正确反映C方法参数,即更改此行
data: "{'fname':'" + document.getElementById('txtCategory').value + "'}",
到
data属性应该与action方法签名中的参数匹配。然后我得到错误405:method Not Allowed这是我需要知道的。但是我找不到。我收到一条警告文本,上面写着ErrorThis is Not help:/。我认为错误在于data属性与action方法签名中的参数不匹配,它的值是fname而不是CategoryName。@chridam您是对的。请将此作为答案发布,以便我可以正确标记您的答案现在谈论性能问题,这段代码是否会在每次函数调试时连接到sql?这不是一种错误的做法吗?
data: "{'CategoryName':'" + document.getElementById('txtCategory').value + "'}",