0

I'm trying to use the Ajax autoCompleteExtender to auto complete my textboxes using a sql query in my code behind. The problem I'm having is that my code behind isn't being hit at all.

What happens instead is my autocomplete options display as the code from my masterpage.

I've done some research and the problem might be that my textboxes are in an ascx file called from my aspx file but I can't figure out a way around it. I've even tried pointing the ServicePath to a webService and it still doesn't hit.

Any input would help, thank you!

Here's my relevant code:

Aspx File:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/Admin.Master" AutoEventWireup="true" EnableEventValidation="false" %>
<%@ Register TagPrefix="AdminSearch" TagName="AdminSearch" Src="AdminSearch.ascx" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<AdminSearch:AdminSearch runat="server" ID="AdminSearch" />
</asp:Content>

Ascx File:

<%@ Control Language="C#" ClassName="WebUserControl" AutoEventWireup="true" EnableViewState="true" Inherits="AdminSearch" CodeBehind="AdminSearch.ascx.cs" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<link href="../Style/Search/AdminSearch.css" rel="stylesheet" />
<script src="../Scripts/js/Search/AdminSearch.js"></script>

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true">
</asp:ScriptManager>

<td class="g3">
        <br />
        <asp:TextBox ID="TextBoxTierProfile" CssClass="g3" runat="server"></asp:TextBox><br />
        <cc1:AutoCompleteExtender ServiceMethod="SearchProfiles" ServicePath="~/AutoComplete.asmx"
            MinimumPrefixLength="2"
            CompletionInterval="1" EnableCaching="true" CompletionSetCount="10"
            TargetControlID="TextBoxTierProfile" 
            ID="AutoCompleteExtender2" runat="server" FirstRowSelected="false">
        </cc1:AutoCompleteExtender>
    </td>

Asmx File:

using System.Collections.Generic;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;

namespace InternalTools.Admin.Search
{
    /// <summary>
    /// Summary description for AutoComplete
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class AutoComplete : System.Web.Services.WebService
    {
        static string sConnection;

        public AutoComplete()
        {
            if (HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"].ToString().Contains("127.0.0.1"))
            {
                sConnection = ConfigurationManager.ConnectionStrings["Reporting"].ToString();
            }
            else
            {
                sConnection = ConfigurationManager.ConnectionStrings["live"].ToString();
            }
        }

        [System.Web.Script.Services.ScriptMethod]
        [WebMethod]
        public static string[] SearchProfiles(string prefixText, int count)
        {

            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = sConnection;
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = (I've cut out the query for security reasons)
                    cmd.Parameters.AddWithValue("@SearchText", prefixText);
                    cmd.Connection = conn;
                    conn.Open();
                    List<string> profiles = new List<string>();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            profiles.Add(sdr["PROFILENAME"].ToString());
                        }
                    }
                    conn.Close();
                    return profiles.ToArray();
                }
            }
        }
    }
}
Rasha
  • 1
  • To check if there is an issue put that SearchProfile method directly in codebehind instead of separate service and specify Service path as a path of that user control. Put a breakpoint and see if that makes it hit breakpoint. – Hakunamatata Jul 01 '16 at 00:13
  • @Hakunamatata Thank you for your input. :) I tried that before trying the separate service and it wasn't hitting the breakpoint at all. – Rasha Jul 01 '16 at 20:09
  • You are using an user control to show autocomplete. You cannot call a WebMethod through user control as control is rendered inside an aspx page. You need to move your WebMethod to your aspx page. – Hakunamatata Jul 02 '16 at 04:01
  • @Hakunamatata Changing the user control to web form fixed this. Thank you – Rasha Jul 29 '16 at 23:40

0 Answers0