Javascript 如何在文本框中的静态方法中获取动态加载数据的id?
我试图获取动态加载的文本框中存在的数据的id 我的问题是我有一个文本框作为搜索框,并在按键时加载数据。代码在这里Javascript 如何在文本框中的静态方法中获取动态加载数据的id?,javascript,c#,jquery,asp.net,sql-server,Javascript,C#,Jquery,Asp.net,Sql Server,我试图获取动态加载的文本框中存在的数据的id 我的问题是我有一个文本框作为搜索框,并在按键时加载数据。代码在这里 [WebMethod] public static string[] GetCustomers(string prefix) { leadsmg ld = new leadsmg(); List<string> customers = new List<string>(); SqlConnec
[WebMethod]
public static string[] GetCustomers(string prefix)
{
leadsmg ld = new leadsmg();
List<string> customers = new List<string>();
SqlConnection conn = new SqlConnection("Data Source=PO-PC\\SQLEXPRESS;Initial Catalog=leadsp;User ID=sa;Password=123;");
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select cname,cid from companydet where cname like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefix);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(string.Format("{0}-{1}", sdr["cname"], sdr["cid"]));
}
}
conn.Close();
}
return customers.ToArray();
}
[WebMethod]
公共静态字符串[]GetCustomers(字符串前缀)
{
leadsmg ld=新的leadsmg();
列出客户=新列表();
SqlConnection conn=newsqlconnection(“数据源=PO-PC\\SQLEXPRESS;初始目录=leadsp;用户ID=sa;密码=123;”;
使用(SqlCommand cmd=new SqlCommand())
{
cmd.CommandText=“从CompanyNet中选择cname,cid,其中cname类似于@SearchText+'%'”;
cmd.Parameters.AddWithValue(“@SearchText”,前缀);
cmd.Connection=conn;
conn.Open();
使用(SqlDataReader sdr=cmd.ExecuteReader())
{
while(sdr.Read())
{
Add(string.Format(“{0}-{1}”、sdr[“cname”]、sdr[“cid”]);
}
}
康涅狄格州关闭();
}
返回客户。ToArray();
}
基于这个文本框,我正在尝试加载另一个文本框,它提供给该特定公司的所有员工,所以我需要上一个文本框的id
用于动态加载数据的下一个文本框的代码为
[WebMethod]
public static string[] GetContactPerson(string prefix)
{
leadsmg ld = new leadsmg();
List<string> customers1 = new List<string>();
SqlConnection conn = new SqlConnection("Data Source=PO-PC\\SQLEXPRESS;Initial Catalog=leadsp;User ID=sa;Password=123;");
using (SqlCommand cmd1 = new SqlCommand())
{
cmd1.CommandText = "select cpid,cpfname,cplname from contactper where (cpfname like @SearchText + '%')and cid='@Cid'";
cmd1.Parameters.AddWithValue("@SearchText", prefix);
cmd1.Parameters.AddWithValue("@Cid", ld.cmpnyid);//hidden field text id ,here i want to access first textbox hidden id to load data from database
cmd1.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd1.ExecuteReader())
{
while (sdr.Read())
{
customers1.Add(string.Format("{0}-{1}", sdr["cpfname"] + " " + sdr["cplname"], sdr["cpid"]));
}
}
conn.Close();
}
return customers1.ToArray();
}
[WebMethod]
公共静态字符串[]GetContactPerson(字符串前缀)
{
leadsmg ld=新的leadsmg();
列表客户1=新列表();
SqlConnection conn=newsqlconnection(“数据源=PO-PC\\SQLEXPRESS;初始目录=leadsp;用户ID=sa;密码=123;”;
使用(SqlCommand cmd1=new SqlCommand())
{
cmd1.CommandText=“从contactper中选择cpid、cpfname、cplname,其中(cpfname类似于@SearchText+'%')和cid='@cid';
cmd1.Parameters.AddWithValue(“@SearchText”,前缀);
cmd1.Parameters.AddWithValue(“@Cid”,ld.cmpnyid);//隐藏字段文本id,这里我想访问第一个文本框隐藏id以从数据库加载数据
cmd1.连接=连接;
conn.Open();
使用(SqlDataReader sdr=cmd1.ExecuteReader())
{
while(sdr.Read())
{
customers1.Add(string.Format(“{0}-{1}”、sdr[“cpfname”]+”+sdr[“cplname”]、sdr[“cpid”]);
}
}
康涅狄格州关闭();
}
返回customers1.ToArray();
}
这是我在网页中使用的java脚本
$(function () {
$("[id$=txtSearchCmpny]").autocomplete({ //textboxid
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/leadsmg.aspx/GetCustomers") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("[id$=cmpnyid]").val(i.item.val);//hidden feild id
},
minLength: 1
});
$(函数(){
$(“[id$=txtSearchCmpny]”。自动完成({//textboxid
来源:功能(请求、响应){
$.ajax({
url:“”,
数据:“{'前缀':'”+request.term+“}”,
数据类型:“json”,
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(数据){
响应($.map)(数据.d,函数(项){
返回{
标签:item.split('-')[0],
val:item.split('-')[1]
}
}))
},
错误:函数(响应){
警报(response.responseText);
},
故障:功能(响应){
警报(response.responseText);
}
});
},
选择:功能(e,i){
$(“[id$=cmpnyid]”.val(i.item.val);//隐藏的字段id
},
最小长度:1
});
如何获取第一个文本框的id以便加载第二个文本框?在上面的JS代码中,您需要第一个文本框的
id
。在select:
callback中需要它吗?我需要第二个文本框中的id以便访问数据库中的数据