我的ASPX代码:
<asp:TextBox ID="txtCollectionCode" runat="server" CssClass="txt" />
<asp:AutoCompleteExtender ID="AutoCompleteCollectionCode" runat="server" TargetControlID="txtCollectionCode" MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="100" ServiceMethod="GetCollectionCode" />
代码背后:
public static List<string>GetCollectionCode(string prefixText)
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("Select Collection_Code From Collections_New WHERE Collection_Code LIKE @Collection_Code+'%'", conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
conn.Close();
List<string> CollectionCodes = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CollectionCodes.Add(dt.Rows[i][1].ToString());
}
return CollectionCodes;
}
我的问题:当我必须绘制名称时,此代码有效。但是当我尝试绘制collection_codes(例如3001,3002,3003)时,从同一个表中没有自动提示。
任何帮助将不胜感激!谢谢
答案 0 :(得分:0)
我想我想通了。
public static ListGetCollectionCode(string prefixText) { string constr = ConfigurationManager.ConnectionStrings [“ConnectionString”]。ToString(); SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("Select * FROM Collections_New WHERE Collection_Code LIKE '" + prefixText + "%'", conn);
DataSet ds = new DataSet();
SqlDataAdapter dta = new SqlDataAdapter(cmd);
dta.Fill(ds);
conn.Close();
List<string> CollectionCodes = new List<string>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
CollectionCodes.Add(ds.Tables[0].Rows[i]["Collection_Code"].ToString());
}
return CollectionCodes;
}