Ajax AutoCompleteExtender没有在userControl C#中遇到代码

时间:2016-06-30 22:50:14

标签: c# asp.net ajax autocomplete ajaxcontroltoolkit

我尝试使用Ajax autoCompleteExtender在我的代码后面使用sql查询自动完成我的文本框。我遇到的问题是我的代码背后根本没有被击中。

相反,我的自动填充选项显示为我的母版页中的代码。

我做过一些研究,问题可能是我的文本框是在我的aspx文件中调用的ascx文件中,但我无法找到解决方法。我甚至尝试将ServicePath指向webService,它仍然没有被击中。

任何输入都会有所帮助,谢谢!

这是我的相关代码:

Aspx文件:

<%@ 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文件:

<%@ 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文件:

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();
                }
            }
        }
    }
}

0 个答案:

没有答案