I have jQuery to pull the autocomplete record as I type in the textbox, this data is being fetched using webservice. Now, everything works fine while using in Default.aspx page, but when I use this process from UserControl.ascx and call it in the default.aspx page then it doesn't return the autocomplete data (result).
WebService.asmx code:
- [WebMethod]
- public List<string> GetAutoCompleteData(string username)
- {
- string conString = ConfigurationManager.ConnectionStrings["MYCONNECTION"].ConnectionString;
- List<string> result = new List<string>();
- using (SqlConnection con = new SqlConnection(conString))
- {
- using (SqlCommand cmd = new SqlCommand("select DISTINCT SSName from SSRecord where SSName LIKE '%'+@SearchText+'%'", con))
- {
- con.Open();
- cmd.Parameters.AddWithValue("@SearchText", username);
- SqlDataReader dr = cmd.ExecuteReader();
- while (dr.Read())
- {
- result.Add(dr["SSName "].ToString());
- }
- return result;
- }
- }
- }
UserControl.ascx code:
- <script src="jquery-1.11.2.js" type="text/javascript"></script>
- <link href="jquery-ui.css" rel="stylesheet" />
- <script src="jquery-ui.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(document).ready(function () {
- SearchText();
- });
- function SearchText() {
- $(".autosuggest").autocomplete({
- source: function (request, response) {
- $.ajax({
- type: "POST",
- contentType: "application/json; charset=utf-8",
- url: "WebService.asmx/GetAutoCompleteData",
- data: "{'username':'" + document.getElementById('txtSearch').value + "'}",
- dataType: "json",
- success: function (data) {
- response(data.d);
- },
- error: function (result) {
- alert("Error");
- }
- });
- }
- });
- }
- </script>
- <label for="tbAuto">Enter UserName: </label>
- <asp:TextBox ID="txtSearch" runat="server" class="autosuggest"></asp:TextBox>
- Default.aspx code:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <%@ Register Src="~/UserControl.ascx" TagPrefix="uc1" TagName="UserControl" %>
- <body>
- <form id="form1" runat="server">
- <div>
- <uc1: UserControl runat="server" ID="UserControl"/>
- </div>
- </form>
- </body>