Kin*_*her 0 c# sql-server asp.net jquery json
我有两个文本框,我正在尝试填充Textbox1的更改事件中的第二个文本框
例如:当我在TextBox1输入客户代码时,我必须填写客户名称.
我的剧本:
$(document).ready(function() {
userCodeChangeEvent();
});
function userCodeChangeEvent() {
$('#<%=txtCustomerCode.ClientID %>').change(function() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Customer_Bill.aspx/GetNameByCode",
data: "{'CusName':'" + document.getElementById('<%=txtCustomerCode.ClientID %>').value + "'}",
dataType: "json",
success: function(data) {
$('#<%=txtcustomerName.ClientID %>').value = data.d;
},
error: function(result) {
alert("No Match");
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
C#:
[WebMethod]
public static string GetNameByCode(string CusName)
{
// List<string> empResult = new List<string>();
string empResult = "";
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Constring"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select user_name From tbl_member_Registration where user_code=@SearchEmpName";
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@SearchEmpName", CusName);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
empResult=dr["user_name"].ToString();
}
con.Close();
return empResult;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我如何解决这个问题?
我应该注意到客户端2件事:
1- $(...).val(data.d);
2-使用" keyup"代替" change"因为在用户输入时不会触发更改.
在服务器端确保通过将[System.Web.Script.Services.ScriptService]属性添加到Web服务类(asmx后面的代码)来启用"ScriptService",如下所示:
[System.Web.Script.Services.ScriptService]
public class WS : System.Web.Services.WebService
{
[WebMethod]
public static string GetNameByCode(string CusName)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
143 次 |
| 最近记录: |