Asp.net ascx代码没有调用它的webMethod,但是当我在正常的aspx页面中使用它时,它工作得很好

Asp.net ascx代码没有调用它的webMethod,但是当我在正常的aspx页面中使用它时,它工作得很好,asp.net,Asp.net,我的代码不起作用,但当我将其与普通asp页面一起使用时,它可以正常工作 [System.Web.Script.Services.ScriptMethod()] [System.Web.Services.WebMethod] public static List<string> GetCity(string prefixText, string contextKey) { DataTable dt = new DataTable();

我的代码不起作用,但当我将其与普通asp页面一起使用时,它可以正常工作

[System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetCity(string prefixText, string contextKey)
    {
        DataTable dt = new DataTable();
        string constr = ConfigurationManager.ConnectionStrings["ERPConnection"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        string CmdText = "select name+ '-' + ' ['+CONVERT(VARCHAR, custid) +']'as name from ht_cust where name like @City+'%' and EmpID =@EmpId";
        SqlCommand cmd = new SqlCommand(CmdText, con);
        cmd.Parameters.AddWithValue("@City", prefixText);
        cmd.Parameters.AddWithValue("@EmpId", contextKey);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
        List<string> CityNames = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            CityNames.Add(dt.Rows[i][0].ToString());
        }
    return CityNames;
    }
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
公共静态列表GetCity(字符串prefixText,字符串contextKey)
{
DataTable dt=新的DataTable();
string constr=ConfigurationManager.ConnectionString[“ERPConnection”].ToString();
SqlConnection con=新的SqlConnection(cont);
con.Open();
string CmdText=“选择name+'-'+'['+CONVERT(VARCHAR,custid)+']”作为来自ht_cust的名称,其中名称如@City+'%'和EmpID=@EmpID”;
SqlCommand cmd=新的SqlCommand(CmdText,con);
cmd.Parameters.AddWithValue(“@City”,前缀);
cmd.Parameters.AddWithValue(“@EmpId”,contextKey);
SqlDataAdapter adp=新的SqlDataAdapter(cmd);
自动进料(dt);
List CityNames=新列表();
对于(int i=0;i
ascx码

<asp:UpdatePanel ID="UpdatePanel7" runat="server">
    <ContentTemplate>
        <asp:TextBox ID="txtCity" runat="server" UseContextKey="true" onkeyup="SetContextKey()"
            CssClass="input-1" Width="200px"></asp:TextBox>
        <ajaxtoolkit:autocompleteextender id="AutoCompleteExtender1" runat="server" targetcontrolid="txtCity"
            minimumprefixlength="1" enablecaching="true" completionsetcount="1" completioninterval="1000"
            servicemethod="GetCity" usecontextkey="true" completionlistcssclass="autocomplete_completionListElement">
                                                        </ajaxtoolkit:autocompleteextender>
    </ContentTemplate>
</asp:UpdatePanel>

我希望您在第页的scriptmanager中添加了属性EnablePageMethods=“true”
并将属性UpdateMode=“Conditional”添加到您的updatepanel中

检查有关为什么不能调用
ascx用户控件中定义的
web方法
的详细信息
有人能帮我吗?为什么要“在用户控件中”使用web方法?DeoWalk我正在使用web方法从数据库中自动完成文本框我已经成为一个严重的问题。好的,我已经尝试过了,但我的scriptmanager在母版页上,所以在这种情况下它会起作用。然后尝试在ascx上添加ScriptManagerProxy,然后尝试上述解决方案。您能解释一下如何添加ScriptManagerProxy吗?您是否在请求时在代码或FireBug中遇到某种错误