ascx代码没有调用它的webMethod为什么,但是当我在普通的aspx页面中使用它时它工作正常

时间:2014-11-26 09:45:42

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

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>

1 个答案:

答案 0 :(得分:0)

我跳了你添加了属性EnablePageMethods =&#34; true&#34;到页面上的脚本管理器 并添加属性UpdateMode =&#34;条件&#34;到您的更新面板