我无法使用Array中的listItems填充下拉列表。我正在创建一个搜索栏来搜索我从存储在数组中的数据库中检索到的项目。我试了一下,下面是我提出来的。
单击按钮时没有任何反应。没有例外或错误。
提前致谢!
public partial class Client_Default : System.Web.UI.Page
{
List<string> clients = new List<string>();
List<string> id = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection cnn = new SqlConnection("Data Source=EDITOR1;Initial Catalog=info;Integrated Security=True"))
{
SqlDataAdapter da = new SqlDataAdapter("select name from Client", cnn);
DataSet ds = new DataSet();
da.Fill(ds, "Client");
SqlDataAdapter da2 = new SqlDataAdapter("select clientId from Client", cnn);
DataSet ds2 = new DataSet();
da.Fill(ds2, "Client");
foreach (DataRow row in ds.Tables["Client"].Rows)
{
clients.Add(row["name"].ToString());
}
foreach (DataRow row in ds2.Tables["Client"].Rows)
{
clients.Add(row["clientId"].ToString());
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int x = 0; x > (clients.Count()-1); x++)
{
if (clients[x].StartsWith(TextBox1.Text))
{
ListItem a = new ListItem(clients[x], id[x]);
DropDownList1.Items.Add(a);
}
else
{
}
}
}
答案 0 :(得分:0)
问题我看到的是你的循环
for (int x = 0; x > (clients.Count()-1); x++)
应该是
for (int x = 0; x < clients.Count(); x++)
此外,
您不需要两次查询数据库并迭代结果两次。