【发布时间】:2014-02-19 19:08:30
【问题描述】:
我有 2 个这样的列表字符串:
List<string> answerList = new List<string>();
List<string> choiceList = new List<string>();
answerList 已添加,其中包含字符串 choiceList 是我要在面板中添加每个文本框的文本的列表
我在面板和每个文本框中有许多文本框,用户在每个文本框中输入文本 然后当用户单击检查按钮时,我想做一个 foreach 循环来访问循环控件。
所以我想做一个 foreach 循环来循环文本框控件文本并将其与 answerList 进行比较,如果不同,它将更改文本框背景颜色。
我设法做到了直到这里(它甚至没有进入 IF 语句):
protected void btnCheck_Click(object sender, EventArgs e)
{
foreach (Control s in Panel1.Controls)
{
//it won't enter here.
if (s.GetType() == typeof(TextBox))
{
TextBox tb = s as TextBox;
choiceList.Add(tb.Text.Trim());
//Compare here but i don't know how .
}
}
}
仅供参考,文本框是动态创建并添加到 Panels 的。
我需要帮助,因为我以前这样做过,但我忘记了我有多久没有编程了......
编辑
我的其余代码(部分,我创建控件并将它们添加到面板的方式):
dr = cmd.ExecuteReader();
while (dr.Read())
{
Label question = new Label();
question.Text = dr["question"].ToString();
Panel1.Controls.Add(question);
LiteralControl lc = new LiteralControl();
lc.Text = "    ";
Panel1.Controls.Add(lc);
TextBox answer = new TextBox();
answer.Width = 100;
Panel1.Controls.Add(answer);
LiteralControl lc1 = new LiteralControl();
lc1.Text = " <br />";
Panel1.Controls.Add(lc1);
answerList.Add(dr["answer"].ToString());
}
【问题讨论】:
-
这个
Panel中有Controls吗?你能查一下Controlscount吗? -
当我检查foreach循环的控件时,它只循环一次@AnatoliiGabuza
标签: c# string list foreach textbox