如何在文本框中出现的静态方法中获取动态加载数据的id?

时间:2017-01-30 04:40:44

标签: 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>();

        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();

    }

并且基于这个文本框,我试图加载另一个文本框,该文本框给出了该公司的所有工作人员,所以我需要在前一个文本框中输入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();
    }

这是我在我的网页中使用的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以便我可以加载我的第二个文本框?

0 个答案:

没有答案