2

I am trying to put Ajax Autocomplete extender method in my child page using page method.Somehow page method is not firing up and instead page holds for a second saying long script is executing and shows some random page mark up in targeted textbox.

I tried setting context key parameter but it didn't work. I even set my code behind file path in sevicepath attribute of extender but that too didn't work

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" 
    CodeBehind="FleetBooking.aspx.cs" Inherits="TMSAdmin.FleetBooking" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
    TagPrefix="CC1" %>

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

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <head runat="server"></head>
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" >
    </asp:ScriptManager>

    <section class="content-header">
        <h1>Fleet booking
            <small>Preview page</small>
        </h1>
    </section>
    <div class="row">
        <div class="col-md-2">
            <h4>Route Name:</h4>
        </div>
        <div class="col-md-2">
            <asp:TextBox ID="txtRoutes" runat="server" CssClass="form-control" />
            <CC1:AutoCompleteExtender ServiceMethod="GetRoutes" MinimumPrefixLength="1" 
                CompletionInterval="100"
                EnableCaching="false" CompletionSetCount="10"
                TargetControlID="txtRoutes" 
                ID="AutoCompleteExtender2"
                runat="server" FirstRowSelected="false"
                CompletionListCssClass="autocomplete_completionListElement"
                CompletionListItemCssClass="autocomplete_listItem"
                CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem">
            </CC1:AutoCompleteExtender>
        </div>
</asp:Content>

Code behind:

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static List<string> GetRoutes(string prefixText, int count)
{
    using (SqlConnection con = new SqlConnection())
    {
        //con.ConnectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
        con.ConnectionString = ClsCon.myconn;
        using (SqlCommand com = new SqlCommand())
        {
            com.CommandText = "select Route_Name from tbl_RouteMaster where "
               + "Route_Name like ' @Search + '%'";

            com.Parameters.AddWithValue("@Search", prefixText);
            com.Connection = con;
            con.Open();
            List<string> routeNames = new List<string>();
            using (SqlDataReader sdr = com.ExecuteReader())
            {
                while (sdr.Read())
                {
                    routeNames.Add(sdr["Route_Name"].ToString());
                }
            }
            con.Close();
            return routeNames;
        }
    }
}

I expect it should fire my page method so that i can debug and solve issues if any.

wazz
  • 4,953
  • 5
  • 20
  • 34
Amol Kadam
  • 85
  • 1
  • 10
  • 1
    `GetRoutes` is not being called at all? – wazz Jul 19 '19 at 21:30
  • 1
    If it's not working I would start by removing the extender and the `Register` directive at the top of the page and re-add the extender, just to make sure that's working. Also make sure the ACToolkit dll is in the Bin. Just as a starting point. – wazz Jul 19 '19 at 21:47
  • 1
    Is it a normal asp.net application or any web service? Try removing the [System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] – Ajoe Jul 22 '19 at 06:48
  • 1
    Yes GetRoutes is not being hit at all and its a asp .net application.I have added [System.Web.Services.WebMethod] for ajax autoextenderto be worked. – Amol Kadam Jul 23 '19 at 18:24
  • 1
    Not sure if it's relevant but try removing the `` element. – wazz Jul 25 '19 at 04:26
  • is required otherwise it throws ajax toolkit error – Amol Kadam Jul 26 '19 at 16:39

2 Answers2

1

A similar issue is discussed here but I couldn't make the proposed solution to work.

I was able to implement with an alternative approach using a web service. Add the following code in WebService1.asmx.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebFormsProject
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public List<string> GetRoutes(string prefixText)
        {
            var list = new List<string>();
            list.Add("Item 1");
            list.Add("Item 2");

            return list;
        }
    }
}

Update the AutoCompleteExtender as follows:

<CC1:AutoCompleteExtender 
    ServiceMethod="GetRoutes" ServicePath="~/WebService1.asmx"
    MinimumPrefixLength="1" 
    CompletionInterval="100"
    EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtRoutes" 
    ID="AutoCompleteExtender2"
    runat="server" FirstRowSelected="false"
    CompletionListCssClass="autocomplete_completionListElement"
    CompletionListItemCssClass="autocomplete_listItem"
    CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem">
</CC1:AutoCompleteExtender>

With the above code I was able to have results in the textbox.

Hope this helps.

Tasos K.
  • 7,979
  • 7
  • 39
  • 63
0

I think there's a mistake in the SQL:

+ "Route_Name like ' @Search + '%'";    // missing double-quote.

should be

+ "Route_Name like '" + @Search + "%'";

or

+ "Route_Name like '%" + @Search + "%'";
wazz
  • 4,953
  • 5
  • 20
  • 34
  • No that is the SQL parameter i am passing its not a c# variable. – Amol Kadam Jul 17 '19 at 05:39
  • Ah of course. Try removing the first single quote before the param from your original. (Not positive...) You might need to replace it with a `+`. – wazz Jul 17 '19 at 07:35