2014-11-25 20 views
0

我的AJAX自動完成功能未與用戶控制工作,但是當我有一個正常的ASP.NET頁面中使用它,它工作得很好:AJAX自動完成不在用戶控件內部工作爲什麼?

[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 [email protected]"; 
    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; 
} 

aspx code 
<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> 
+0

請幫助我。 – 2014-11-25 12:41:22

+0

代碼在哪裏? – xxxx 2014-11-25 12:42:22

+0

先分享你的代碼 – 2014-11-25 12:51:25

回答

1

您無法通過用戶控件調用webmethod,因爲它會在頁面內自動呈現。將您的webmethod移至您的aspx頁面。

如果你想要控制器內部的邏輯,那麼你可以從aspx頁面調用它,但你的webmethod需要在aspx頁面。

例子:

在aspx頁面:

[System.Web.Script.Services.ScriptMethod()] 
[System.Web.Services.WebMethod] 
public static List<string> GetCity(string prefixText, string contextKey) 
{ 
    return mycontrol.GetCity(prefixText, contextKey); 
} 

在用戶控件:

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 [email protected]"; 
    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; 
} 
+0

先生如何在aspx.cs頁面創建mycontrol的對象 – 2014-11-26 05:43:09

+0

請問能回答這個問題嗎? – 2014-11-26 06:10:02

+0

zaki先生你可以解釋你是如何創建用戶控制內的網絡方法 – 2014-11-26 07:42:51