user3781993 user3781993 - 1 month ago 10
C# Question

ajax auto complete textbox is not working when 2 autocomplete texboxes are present in same page

I have a page where 180 items need to be searched for 1 text box and 200 items for 2nd textbox. Auto complete is working fine for 1st textbox and not giving auto complete option for 2nd textbox here is my code.

<td>
<asp:TextBox ID="TB_DeptID" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender
runat="server"
ID="AC_Dept"
TargetControlID="TB_DeptID"
CompletionListElementID="divwidth"
MinimumPrefixLength="1"
CompletionInterval="10"
EnableCaching="true"
CompletionSetCount="12"
ServiceMethod="GetDept" ></asp:AutoCompleteExtender>
</td>

<td>
<asp:TextBox ID="TB_chargeHdId" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender
runat="server"
ID="CH_autocomplete"
TargetControlID="TB_chargeHdId"
CompletionListElementID="divwidth"
MinimumPrefixLength="1"
CompletionInterval="10"
EnableCaching="true"
CompletionSetCount="12"
ServiceMethod="GetchrgHd_Id"></asp:AutoCompleteExtender>

</td>


and in code behind

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetchrgHd_Id(string prefixText)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
// select custaddress1 from tblcustomer
SqlCommand cmd = new SqlCommand("select top 10 Charge_Head_Id from Charge_Head_M where Charge_Head_Id like '" + prefixText + "'+'%'", con);
//cmd.Parameters.AddWithValue("@itemname_code", prefixText);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
List<string> custaddress1 = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
custaddress1.Add(dt.Rows[i]["Charge_Head_Id"].ToString());
}
return custaddress1;
}

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]

public static List<string> GetDept(string prefixText1)
{

DataTable dt1 = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
// select custaddress1 from tblcustomer
SqlCommand cmd = new SqlCommand("select top 10 Dept_Id from Department_M where Dept_Id like '" + prefixText1 + "'+'%'", con);
//cmd.Parameters.AddWithValue("@itemname_code", prefixText);
SqlDataAdapter adp1 = new SqlDataAdapter(cmd);
adp1.Fill(dt1);
List<string> custaddress2 = new List<string>();
for (int j = 0; j < dt1.Rows.Count; j++)
{
custaddress2.Add(dt1.Rows[j]["Dept_Id"].ToString());
}
return custaddress2;
}


how to resolve this??

Answer

change prefixtext1 to prefixtext in getdept method.