locked
AutoCompleteExtender displays html source character by character of the current page as autocomplete suggestion list RRS feed

  • Question

  • User-1915458 posted

    My autcomplete extender is displaying html page source instead of the database results.

    I have isolated the issue to when I load the Microsoft.AspNet.FriendlyUrls.dll and enable routing in the Global.asax file.

    As soon as I comment out the line " RouteConfig.RegisterRoutes(RouteTable.Routes);" in the Global.asax the autocomplete will display correctly.

    Any ideas on how to fix this?  Is it a routing bug, or a ajaxcontroltoolkit bug?

    Thanks.

    Sunday, June 26, 2016 5:12 PM

Answers

  • User177399542 posted

    Hi

    I have found solution:

    1) Create a new .asmx webservice and move your code to it. Then call method from service. Here is tested and working code:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    /// <summary>
    /// Summary description for myWebservice
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class myWebservice : System.Web.Services.WebService
    {
    
        public myWebservice()
        {
    
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }
    
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public List<string> SearchCustomers(string prefixText, int count)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                //conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["TBDConnString"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand())
                {
                    //cmd.CommandText = "select ContactName from Customers where ContactName like @SearchText + '%'";
                    cmd.CommandText = "select distinct Institution_Name from Accreditation_2016_03 where Institution_Name like '%' + @SearchText + '%'";
    
                    cmd.Parameters.AddWithValue("@SearchText", prefixText);
                    cmd.Connection = conn;
                    conn.Open();
                    List<string> customers = new List<string>();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            //customers.Add(sdr["ContactName"].ToString());
                            customers.Add(sdr["Institution_Name"].ToString());
                        }
                    }
                    conn.Close();
                    return customers;
                }
            }
    
        }
    }
    
    <asp:TextBox ID="txtContactsSearch" runat="server" Width="400px" />
    
                <ajaxToolkit:AutoCompleteExtender ServicePath="myWebservice.asmx" ServiceMethod="SearchCustomers" MinimumPrefixLength="2"
                    ID="AutoCompleteExtender1" runat="server"
                    CompletionInterval="100"
                    EnableCaching="false"
                    CompletionSetCount="10"
                    TargetControlID="txtContactsSearch"
                    FirstRowSelected="false">
                </ajaxToolkit:AutoCompleteExtender>
                <ajaxToolkit:TextBoxWatermarkExtender runat="server"
                    BehaviorID="TextBox1_TextBoxWatermarkExtender"
                    TargetControlID="txtContactsSearch"
                    ID="TextBox1_TextBoxWatermarkExtender"
                    WatermarkText="Start Typing Your University Name" />



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 29, 2016 6:43 AM

All replies

  • User177399542 posted

    Hi

    The issue is with combination of  FriendlyUrls and ajax control toolkit try this:

    http://stackoverflow.com/questions/1306605/tearing-my-hair-out-asp-net-ajax-autocomplete-not-working

    Monday, June 27, 2016 4:28 AM
  • User-1915458 posted

    Thank you for that link.

    They say they solved the issue by "I've added in some code to Global.asax which only rewrites the url if it has a file extension other than .axd. So if it has .aspx or something else according to the re-write rules it gets checked and re-written, if not it gets left alone. "

    How would I write that in the Global.asax file to not rewrite?

    Right now all I have is this:

     void Application_Start(object sender, EventArgs e)
        {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

    Thank you for all your help!

    Monday, June 27, 2016 3:44 PM
  • User-1915458 posted

    Below is all my code that is having the issue:

    AutoComplete.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoComplete.aspx.cs" Inherits="AutoComplete" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
         <style type="text/css">
            .loading {
                background-image: url(images/Animated16x16/loader.gif);
                background-;
                background-repeat: no-repeat;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
       <asp:ScriptManager ID="ScriptManager1" runat="server" />
    
                <asp:TextBox ID="txtContactsSearch" runat="server" Width="400px"/>
    
                <ajaxToolkit:AutoCompleteExtender ServiceMethod="SearchCustomers" MinimumPrefixLength="2"
                    ID="AutoCompleteExtender1" runat="server" 
                    CompletionInterval="100" 
                    EnableCaching="false" 
                    CompletionSetCount="10" 
                    TargetControlID="txtContactsSearch"
                    FirstRowSelected="false" 
                    OnClientHiding="OnClientCompleted"
                    OnClientPopulated="OnClientCompleted" 
                    OnClientPopulating="OnClientPopulating">
                </ajaxToolkit:AutoCompleteExtender>
                <ajaxToolkit:TextBoxWatermarkExtender runat="server" 
                    BehaviorID="TextBox1_TextBoxWatermarkExtender" 
                    TargetControlID="txtContactsSearch" 
                    ID="TextBox1_TextBoxWatermarkExtender" 
                    WatermarkText="Start Typing Your University Name"/>
    
                <script type="text/javascript">
                    function OnClientPopulating(sender, e) {
                        sender._element.className = "loading";
                    }
                    function OnClientCompleted(sender, e) {
                        sender._element.className = "";
                    }
                </script>
        </div>
        </form>
    </body>
    </html>
    
    

    Also tried adding ServicePath="~/" to the autocomplet but it didn't work.

    AutoComplete.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Script;
    using System.Web.Services;
    
    public partial class AutoComplete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        //[System.Web.Script.Services.ScriptMethod()]
        //[System.Web.Services.WebMethod]
        [System.Web.Services.WebMethodAttribute, System.Web.Script.Services.ScriptMethod]
        public static List<string> SearchCustomers(string prefixText, int count)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                //conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["TBDConnString"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand())
                {
                    //cmd.CommandText = "select ContactName from Customers where ContactName like @SearchText + '%'";
                    cmd.CommandText = "select distinct Institution_Name from Accreditation_2016_03 where Institution_Name like '%' + @SearchText + '%'";
    
                    cmd.Parameters.AddWithValue("@SearchText", prefixText);
                    cmd.Connection = conn;
                    conn.Open();
                    List<string> customers = new List<string>();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            //customers.Add(sdr["ContactName"].ToString());
                            customers.Add(sdr["Institution_Name"].ToString());
                        }
                    }
                    conn.Close();
                    return customers;
                }
            }
        }
    }
    

    Global.asax

    <%@ Application Language="C#" %>
    <%@ Import Namespace="System.Web.Routing" %>
    
    <script RunAt="server">
    
        void Application_Start(object sender, EventArgs e)
        {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    
    </script>
    

    App_Code\App_Start\RouteConfig.cs

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Routing;
    using Microsoft.AspNet.FriendlyUrls;
    
    namespace ASP
    {
        public static class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                var settings = new FriendlyUrlSettings();
                settings.AutoRedirectMode = RedirectMode.Permanent;
                routes.EnableFriendlyUrls(settings);
            }
        }
    }
    

    I also tried commenting out the "settings.AutoRedirectMode = RedirectMode.Permanent;" but that didn't work.

    Thanks.

    Monday, June 27, 2016 9:13 PM
  • User-1915458 posted

    I can't seem to get this to work.  It also won't work with JQuery autocomplete.

    If someone has this working, can you please post the solution?

    Thanks

    Wednesday, June 29, 2016 4:51 AM
  • User177399542 posted

    Hi

    I have found solution:

    1) Create a new .asmx webservice and move your code to it. Then call method from service. Here is tested and working code:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    /// <summary>
    /// Summary description for myWebservice
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class myWebservice : System.Web.Services.WebService
    {
    
        public myWebservice()
        {
    
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }
    
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public List<string> SearchCustomers(string prefixText, int count)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                //conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["TBDConnString"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand())
                {
                    //cmd.CommandText = "select ContactName from Customers where ContactName like @SearchText + '%'";
                    cmd.CommandText = "select distinct Institution_Name from Accreditation_2016_03 where Institution_Name like '%' + @SearchText + '%'";
    
                    cmd.Parameters.AddWithValue("@SearchText", prefixText);
                    cmd.Connection = conn;
                    conn.Open();
                    List<string> customers = new List<string>();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            //customers.Add(sdr["ContactName"].ToString());
                            customers.Add(sdr["Institution_Name"].ToString());
                        }
                    }
                    conn.Close();
                    return customers;
                }
            }
    
        }
    }
    
    <asp:TextBox ID="txtContactsSearch" runat="server" Width="400px" />
    
                <ajaxToolkit:AutoCompleteExtender ServicePath="myWebservice.asmx" ServiceMethod="SearchCustomers" MinimumPrefixLength="2"
                    ID="AutoCompleteExtender1" runat="server"
                    CompletionInterval="100"
                    EnableCaching="false"
                    CompletionSetCount="10"
                    TargetControlID="txtContactsSearch"
                    FirstRowSelected="false">
                </ajaxToolkit:AutoCompleteExtender>
                <ajaxToolkit:TextBoxWatermarkExtender runat="server"
                    BehaviorID="TextBox1_TextBoxWatermarkExtender"
                    TargetControlID="txtContactsSearch"
                    ID="TextBox1_TextBoxWatermarkExtender"
                    WatermarkText="Start Typing Your University Name" />



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 29, 2016 6:43 AM
  • User-1915458 posted

    That does work, thank you very much for this.

    Is there anyway to get around having to create a separate webservice page?

    Friday, July 1, 2016 4:09 AM
  • User177399542 posted

    Yes you can create separate web service. In the code given I have create .asmx service.

    Friday, July 1, 2016 9:05 AM