如何获得选定的ID

时间:2016-02-16 08:15:00

标签: jquery asp.net

在我的要求中,自动填充搜索框就在那里。我从数据库中获取数据。之后,如果我选择了一个项目,则会显示该项目ID。我需要它。提前致谢。请解决这个问题,我需要该ID才能进一步开展工作。

 <link href="CSS/jquery-ui.css" rel="stylesheet" type="text/css" />   
<script src="Scripts/jquery-2.2.0.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-1.11.4.min.js" type="text/javascript</script><script type="text/javascript">
    $(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "code.aspx?Meth=AB&username=" + document.getElementById('txtSearch').value,
                    data: "{}",
                    dataType: "json",
                    success: function (data) {
                        var list = data;
                        if (list.length > 0) {
                            response($.map(list, function (item) {
                                return {
                                    label: item.split(' ? ')[0],
                                    val: item.split('?')[1]
                                }
                            }));
                        }
                        else {
                            response([{ label: 'No Records Found', val: -1}]);
                        }

                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            },
            select: function (event, ui) {
                if (ui.item.val == -1) {
                    return false;
                }
                //$('#lblUserId').text(ui.item.val);
                alert(ui.item.val);
            }
        });
    }
</script><form  id="form1" class="navbar-form" role="search" >
    <div class="input-group" >
        <input type="text" id="txtSearch"  class="form-control autosuggest" placeholder="Search">
        <div class="input-group-btn" >
            <button class="btn btn-default"  type="submit"><i class="fa fa-search" style="color:brown;"></i></button>

        </div>
    </div>
    </form>protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (HttpContext.Current.Request.QueryString["Meth"] != null && HttpContext.Current.Request.QueryString["Meth"].ToString() != "")
        {
            string Req = Request.QueryString["Meth"].ToString();
            if (Req == "AB")
            {
                List<string> lst1 = new List<string>();
                string username = HttpContext.Current.Request.QueryString["username"].ToString();
                lst1 = GetAutoCompleteData(username);
                if (lst1 != null)
                {
                    string result = JsonConvert.SerializeObject(lst1);
                    HttpResponse res1 = HttpContext.Current.Response;
                    res1.Clear();
                    res1.BufferOutput = true;
                    res1.StatusCode = 200;
                    res1.Write(result);
                    res1.ContentType = "text/json";
                    res1.End();


                }
            }
        }
    }

}
public static List<string> GetAutoCompleteData(string username)
{
    List<string> result = new List<string>();
    using (SqlConnection con = new SqlConnection("Data Source=192.168.1.42,1433;Initial Catalog=Harneedi;User ID=chaitanya_t;Password=makrotech"))
    {
        using (SqlCommand cmd = new SqlCommand("select specializationID,specialization,educationID from Mast_Specialization where specialization LIKE '%'+@SearchText+'%'", con))
        {
            con.Open();
            cmd.Parameters.AddWithValue("@SearchText", username);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                // ID also display bcz of {1}
                //result.Add(string.Format("{0},{1}", dr["specialization"], dr["specializationID"]));

                result.Add(string.Format("{0},{1}", dr["specialization"], dr["specializationID"]));
            }
            return result;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我看到的唯一问题是:

label: item.split(' ? ')[0],

split方法期望一个字符串,其中包含前导和尾随' ? '的空格。您可以像对待其他人一样删除它:

label: item.split('?')[0],

另一个似乎是一个错字,可能是你错过了ui lib脚本的结束:

<script src="Scripts/jquery-ui-1.11.4.min.js" type="text/javascript"></script>
                                                      <!--here-----^^-->