如何解决在C#中对select事件和WebMethod进行jQuery自动完成
我正在使用jQuery autocomplete
,它工作正常。
现在,我要由用户使用选定的值并执行所需的操作。
在从AutoComplete TextBox
的预填充列表中选择值的上一步中,我们将调用jQuery GetCustomerDetails
函数。
将Country
名称作为输入参数,将ajax call
更改为WebMethod get Customer
并在表对象中呈现。
这是正在使用的代码,我没有错误,但是由于tblCustomers
表仍然为空...
我该怎么办?
.aspx页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function () {
$("#txtCountry").autocomplete({
source: function (request,response) {
var param = { keyword: $('#txtCountry').val() };
$.ajax({
url: "Default4.aspx/GetCountryNames",data: JSON.stringify(param),dataType: "json",type: "POST",contentType: "application/json; charset=utf-8",dataFilter: function (data) { return data; },success: function (data) {
response($.map(data.d,function (item) {
return {
value: item
}
}))
},error: function (XMLHttpRequest,textStatus,errorThrown) {
alert(textStatus);
}
});
},select: function (event,ui) {
if (ui.item) {
GetCustomerDetails(ui.item.value);
}
},minLength: 2
});
});
function GetCustomerDetails(country) {
$("#tblCustomers tbody tr").remove();
$.ajax({
type: "POST",url: "Default4.aspx/GetCustomers",data: '{country: "' + country + '" }',success: function (data) {
response($.map(data.d,function (item) {
var rows = "<tr>"
+ "<td class='customertd'>" + item.Name + "</td>"
+ "<td class='customertd'>" + item.Population + "</td>"
+ "<td class='customertd'>" + item.Code + "</td>"
+ "<td class='customertd'>" + item.Continent + "</td>"
+ "<td class='customertd'>" + item.Region + "</td>"
+ "<td class='customertd'>" + item.GovernmentForm + "</td>"
+ "</tr>";
$('#tblCustomers tbody').append(rows);
}))
},failure: function (response) {
alert(response.d);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Country Name:
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
</div>
<br />
<div>
<table id="tblCustomers" class="tblCustomers">
<thead>
<tr>
<th class="customerth">Name</th>
<th class="customerth">Population</th>
<th class="customerth">Code</th>
<th class="customerth">Continent</th>
<th class="customerth">Region</th>
<th class="customerth">GovernmentForm</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</form>
</body>
</html>
.cs页面
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default4 : System.Web.UI.Page
{
public class Customer
{
public string Name { get; set; }
public string Population { get; set; }
public string Code { get; set; }
public string Continent { get; set; }
public string Region { get; set; }
public string GovernmentForm { get; set; }
}
[WebMethod]
public static Customer[] GetCustomers(string country)
{
List<Customer> customers = new List<Customer>();
string CS = ConfigurationManager.ConnectionStrings["cn3"].ConnectionString;
string query = string.Format("SELECT `Name`,`Population`,`Code`,`Continent`,`Region`,`GovernmentForm` FROM `country` WHERE `Name` LIKE '%{0}%';",country);
using (MySqlConnection con =
new MySqlConnection(CS))
{
using (MySqlCommand cmd =
new MySqlCommand(query,con))
{
con.Open();
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Customer customer = new Customer();
customer.Name = reader.GetString(0);
customer.Population = reader.GetString(1);
customer.Code = reader.GetString(2);
customer.Continent = reader.GetString(3);
customer.Region = reader.GetString(4);
customer.GovernmentForm = reader.GetString(5);
customers.Add(customer);
}
}
}
return customers.ToArray();
}
[WebMethod]
public static string[] GetCountryNames(string keyword)
{
List<string> country = new List<string>();
string CS = ConfigurationManager.ConnectionStrings["cn3"].ConnectionString;
string query = string.Format("SELECT DISTINCT `Name` FROM `country` WHERE `Name` LIKE '%{0}%';",keyword);
using (MySqlConnection con =
new MySqlConnection(CS))
{
using (MySqlCommand cmd =
new MySqlCommand(query,con))
{
con.Open();
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
country.Add(reader.GetString(0));
}
}
}
return country.ToArray();
}
protected void Page_Load(object sender,EventArgs e)
{
}
}
编辑问题
使用断点并调试此零件代码中的错误,
未定义响应
response($.map(data.d,function (item) {
var rows = "<tr>"
+ "<td class='customertd'>" + item.Name + "</td>"
+ "<td class='customertd'>" + item.Population + "</td>"
+ "<td class='customertd'>" + item.Code + "</td>"
+ "<td class='customertd'>" + item.Continent + "</td>"
+ "<td class='customertd'>" + item.Region + "</td>"
+ "<td class='customertd'>" + item.GovernmentForm + "</td>"
+ "</tr>";
$('#tblCustomers tbody').append(rows);
}))
解决方案
$.map(data.d,function (item) {
var rows = "<tr>"
+ "<td class='customertd'>" + item.Name + "</td>"
+ "<td class='customertd'>" + item.Population + "</td>"
+ "<td class='customertd'>" + item.Code + "</td>"
+ "<td class='customertd'>" + item.Continent + "</td>"
+ "<td class='customertd'>" + item.Region + "</td>"
+ "<td class='customertd'>" + item.GovernmentForm + "</td>"
+ "</tr>";
$('#tblCustomers tbody').append(rows);
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。