Sunday, 1 February 2015

Auto Complete text box by jquery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css"/>
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css"/>  
   
     <script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
        function SearchText() {
            $(".autosuggest1").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "autocomplete.aspx/Autocomplete",

                        data: "{'search':'" + $('#<%=txtsearch.ClientID %>').val() + "'}",
                        dataType: "json",
                        success: function (data) {
                                  response($.map(data.d, function (item) {
                               
                                return {
                                    label: item.split('/')[1],
                                    val: item.split('/')[0]
                                 
                                }
                               
                            }));
                            },
                                error: function (result) {
                                    alert("please enter other value");
                                }
                     
                    });
                 
                },
                select: function(event, ui) {
                    $('#lblUserId').text(ui.item.val);
                    $('#hdnId').val(ui.item.val);
                   
                }
            });
        }
     
</script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:HiddenField ID="hdnId" runat="server" />
            <input type="text" id="txtsearch" class="autosuggest1" runat="server"/>
        <div>
Selected UserId:<b><label id="lblUserId" /></b>
</div>
        <asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClientClick="checkhdnvalue();" OnClick="btnsubmit_Click" />
       </div>
    </form>
</body>
</html>
[WebMethod]
        public static List<string> Autocomplete(string search)
        {int i;
            List <string> result= new List<string>();
               //  int _totalrowcount = 0;
                string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
                   SqlParameter[] prm = new SqlParameter[1];
                prm[0] = new SqlParameter("@search", search);

                DataSet ds = SqlHelper.ExecuteDataset(connStr, CommandType.StoredProcedure, "sp_autocomplete", prm);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        result.Add(ds.Tables[0].Rows[i][0].ToString() + '/' + ds.Tables[0].Rows[i][1].ToString());
                    }
                }
                else {
                    result.Add("0/no record found");
                }
                return result;
       
        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            if (hdnId.Value =="0")
            {
                Response.Write("plesase enter value");

                txtsearch.Value = String.Empty;
            }
        }




No comments:

Post a Comment